2012-06-27 48 views
0

我已經創建了這個簡單的例子來說明我想要完成的事情。我需要與Android應用程序生命週期的協調

這是我的第一個佈局:

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.*; 
import android.widget.RadioGroup.OnCheckedChangeListener; 

public class LifeCycleActivity extends Activity implements OnCheckedChangeListener { 
/** Called when the activity is first created. */ 

private RadioButton rbR, rbG, rbB; 
private RadioGroup rg; 
private Button next; 
int color=0; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    rbR = (RadioButton) findViewById(R.id.radio0); 
    rbB = (RadioButton) findViewById(R.id.radio1); 
    rbG = (RadioButton) findViewById(R.id.radio2); 
    rg = (RadioGroup) findViewById(R.id.radioGroup1); 
    next = (Button) findViewById(R.id.button1); 

    final Intent it = new Intent(this, next.class); 
    final Bundle b = new Bundle(); 

    rg.setOnCheckedChangeListener(this); 
    next.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      b.putInt("color", color); 
      it.putExtras(b); 
      startActivity(it); 
     } 
    }); 
} 

public void onCheckedChanged(RadioGroup group, int checkedId) { 
    // TODO Auto-generated method stub 
    if (checkedId==rbR.getId()) color=1; 
    if (checkedId==rbB.getId()) color=2; 
    if (checkedId==rbG.getId()) color=3; 
} 

}

這是第二次佈局:

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.View; 
import android.widget.*; 

public class next extends Activity { 

private LinearLayout ll; 
private ImageView im; 
private TextView tv; 
private Button save; 
private Bundle extras; 
private int color=0; 
private String selColor=""; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.next); 

    ll = (LinearLayout) findViewById(R.id.mainll); 
    ll.setOrientation(LinearLayout.VERTICAL); 
    im = new ImageView(this); 
    tv = new TextView(this); 
    save = new Button(this); 
    save.setText("save"); 

    extras = getIntent().getExtras(); 
    color = extras.getInt("color"); 

    im.setImageResource(R.drawable.ic_launcher); 
    ll.addView(im); 

    if (color == 1) selColor = "RED"; 
    if (color == 2) selColor = "BLUE"; 
    if (color == 3) selColor = "GREEN"; 
    tv.setText(selColor); 
    tv.setGravity(Gravity.CENTER); 
    ll.addView(tv); 
    ll.addView(save); 

    save.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      // here i want to save and exit 
      // so i can call onPause(), then finish() 
      // do not know how exactly since i have to follow some goals 
      // that i need for this example 
     } 
    }); 
} 

}

我也有main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<RadioGroup 
    android:id="@+id/radioGroup1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <RadioButton 
     android:id="@+id/radio0" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:checked="true" 
     android:text="red" /> 

    <RadioButton 
     android:id="@+id/radio1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="blue" /> 

    <RadioButton 
     android:id="@+id/radio2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="green" /> 
</RadioGroup> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="next" /> 

</LinearLayout> 

和next.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainll" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

</LinearLayout> 

正如你所看到的,根據顏色選擇的第一個佈局我動態生成第二佈局。

這就是我想做的事,幫我請(添加代碼,如果可能的話):在第二佈局

  1. 如果我按保存,我想保存應用程序狀態(無論我建立了一個網頁上)並退出。
  2. 如果我再添加一個按鈕來在該佈局上創建其他動態對象(帶有名稱的EditText或具有不同對象的另一個佈局),如果用戶保存,他/她也將保存新對象(此部分可稍後完成)
  3. 如果用戶保存該頁面一次,然後我希望他能夠獲得他/她在退出前的相同應用程序狀態(當然如果他/她選擇相同的單選按鈕)。
  4. 如果他/她選擇另一個按鈕,我不需要顯示以前的狀態,因爲選擇不同。
+1

使用SQLite數據庫來存儲狀態,這總是最安全的選擇。您可以通過調用onPause()和onStop()方法來始終更新數據庫。 :) –

+0

我也可以用sharedpreferencea保存所有內容,但是我無法正確使用onPause和onStart函數來獲得我想要的結果。謝謝 – Brian

+0

請幫忙.... – Brian

回答

0

sharedpreferences爲我做的訣竅。我終於能夠相同並檢索我的數據和onStart。儘管這很好,但我很欣賞Kevin_Dingo的回答,這是一種非常專業的方式。