2010-09-05 43 views
1

我一直在研究我的應用程序,並且到了需要了解活動生命週期的階段,我不認爲我明白了。我擁有的是一個帶有七個按鈕的活動。當用戶按下按鈕時,會打開一個listActivity,用戶可以在該列表中選擇列表中的項目,將標題發送回調用活動,並且調用活動將該標題旁邊的標題添加到按鈕上的文本中。 (它使用的方法在switch語句中,取決於請求代碼返回到哪個 - 我認爲這對我的目的有點弱)。但是,如果用戶離開活動並返回到它,請通過後退按鈕或任何其他按鈕,當他們返回時沒有任何額外的信息 - 所有剩下的都是帶有原始文本的七個按鈕。我希望信息留下 - 我該如何解決這個問題?Android onPause()或saveInstanceState()?

這裏是從主叫活動的代碼:

package com.dinner.live; 

import android.app.Activity; 
import android.app.ListActivity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.SimpleCursorAdapter; 
import android.widget.Toast; 

public class PlanMenu extends Activity { 

static final private int CHOOSE_MONDAY = 0; 
static final private int CHOOSE_TUESDAY = 1; 
static final private int CHOOSE_WEDNESDAY = 2; 
static final private int CHOOSE_THURSDAY = 3; 
static final private int CHOOSE_FRIDAY = 4; 
static final private int CHOOSE_SATURDAY = 5; 
static final private int CHOOSE_SUNDAY = 6; 
private boolean ButtonPushed = false; 
private NotesDbAdapter mDbHelper; 
private MenuDbAdapter menuDbHelper; 
private Long menuID; 
private String mTitleText; 
private String m; 

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

    setContentView(R.layout.plan_menu); 

    Toast.makeText(this, "Choose a day to pick a meal for!", Toast.LENGTH_LONG).show(); 
    mDbHelper = new NotesDbAdapter(this); 
     mDbHelper.open(); 
     menuDbHelper = new MenuDbAdapter(this); 
     menuDbHelper.open(); 
    } 

public void mButtonHandler(View target) 
    { 
    switch(target.getId()) 
    { 
    case R.id.monday: 
    if(ButtonPushed == false) 
    { 
    // Create new intent object and tell it to call the ColorPicker class 
    Intent monday = new Intent(this, PlanMenuList.class); 
    // Start ColorPicker as a new activity and wait for the result 
    startActivityForResult(monday, CHOOSE_MONDAY); 
    ButtonPushed = true; 
    break; 
    } 
    else 
    Toast.makeText(this, "That button has already been pushed." , Toast.LENGTH_LONG).show(); 
    break; 
    case R.id.tuesday: 
    // Create new intent object and tell it to call the ColorPicker class 
     Intent tuesday = new Intent(this, PlanMenuList.class); 
     // Start ColorPicker as a new activity and wait for the result 
     startActivityForResult(tuesday, CHOOSE_TUESDAY); 
     break; 
    case R.id.wednesday: 
    // Create new intent object and tell it to call the ColorPicker class 
     Intent wednesday = new Intent(this, PlanMenuList.class); 
     // Start ColorPicker as a new activity and wait for the result 
     startActivityForResult(wednesday, CHOOSE_WEDNESDAY); 
     break; 
    case R.id.thursday: 
    // Create new intent object and tell it to call the ColorPicker class 
     Intent thursday = new Intent(this, PlanMenuList.class); 
     // Start ColorPicker as a new activity and wait for the result 
     startActivityForResult(thursday, CHOOSE_THURSDAY); 
     break; 
    case R.id.friday: 
    // Create new intent object and tell it to call the ColorPicker class 
     Intent friday = new Intent(this, PlanMenuList.class); 
     // Start ColorPicker as a new activity and wait for the result 
     startActivityForResult(friday, CHOOSE_FRIDAY); 
     break; 
    case R.id.saturday: 
    // Create new intent object and tell it to call the ColorPicker class 
     Intent saturday = new Intent(this, PlanMenuList.class); 
     // Start ColorPicker as a new activity and wait for the result 
     startActivityForResult(saturday, CHOOSE_SATURDAY); 
     break; 
    case R.id.sunday: 
    // Create new intent object and tell it to call the ColorPicker class 
     Intent sunday = new Intent(this, PlanMenuList.class); 
     // Start ColorPicker as a new activity and wait for the result 
     startActivityForResult(sunday, CHOOSE_SUNDAY); 
     break; 
    } 



    } 


@Override 
    protected void onActivityResult(int requestCode, int resultCode, 
            Intent intent) { 
     super.onActivityResult(requestCode, resultCode, intent); 
     if(resultCode == RESULT_OK){ 
     switch(requestCode) 
     { 
      case CHOOSE_MONDAY: 
      menuID = intent.getExtras().getLong("newRowId"); 
       menuDbHelper.fetchMenu(menuID); 
      populateFields(); 
      //String label = intent.getExtras().getString("menuString");//intent.getExtras().getLong(MenuDbAdapter.KEY_ROWID); 
      Button tv = (Button)findViewById(R.id.monday); 
      m = mTitleText; 
      tv.setText("Monday" + " - " + mTitleText); 
      break; 
      case CHOOSE_TUESDAY: 
      String labelT = intent.getExtras().getString("menuString");//intent.getExtras().getLong(MenuDbAdapter.KEY_ROWID); 
      Button tvT = (Button)findViewById(R.id.tuesday); 
      tvT.setText("Tuesday" + " - " + labelT); 
      break; 
      case CHOOSE_WEDNESDAY: 
      String labelW = intent.getExtras().getString("menuString");//intent.getExtras().getLong(MenuDbAdapter.KEY_ROWID); 
      Button tvW = (Button)findViewById(R.id.wednesday); 
      tvW.setText("Wednesday" + " - " + labelW); 
      break; 
      case CHOOSE_THURSDAY: 
      String labelTh = intent.getExtras().getString("menuString");//intent.getExtras().getLong(MenuDbAdapter.KEY_ROWID); 
      Button tvTh = (Button)findViewById(R.id.thursday); 
      tvTh.setText("Thursday" + " - " + labelTh); 
      break; 
      case CHOOSE_FRIDAY: 
      String labelFriday = intent.getExtras().getString("menuString");//intent.getExtras().getLong(MenuDbAdapter.KEY_ROWID); 
      Button tvFriday = (Button)findViewById(R.id.friday); 
      tvFriday.setText("Friday" + " - " + labelFriday); 
      break;   
      case CHOOSE_SATURDAY: 
      String labelS = intent.getExtras().getString("menuString");//intent.getExtras().getLong(MenuDbAdapter.KEY_ROWID); 
      Button tvS = (Button)findViewById(R.id.saturday); 
      tvS.setText("Saturday" + " - " + labelS); 
      break; 
      case CHOOSE_SUNDAY: 
      String labelSa = intent.getExtras().getString("menuString");//intent.getExtras().getLong(MenuDbAdapter.KEY_ROWID); 
      Button tvSa = (Button)findViewById(R.id.sunday); 
      tvSa.setText("Sunday" + " - " + labelSa); 
      break; 

     } 

     } 
} 

private void populateFields() { 
     if (menuID != null) { 
      Cursor note = menuDbHelper.fetchMenu(menuID); 
      startManagingCursor(note); 
      mTitleText = (note.getString(
       note.getColumnIndexOrThrow(MenuDbAdapter.KEY_TITLE))); 

     } 
    } 




} 
+0

我對你的代碼進行了格式化,但它仍然是一團糟。把它清理一下,把它切成相關的部分,你會得到更好的迴應。 – 2010-09-05 21:27:01

回答

2

取決於。一般來說,你需要重寫onSaveInstanceState()並將所有數據保存到Bundle中(這是一個簡單的鍵/值對事務),並在onCreate()(或onRestoreInstanceState()中重新創建,如果要分離代碼出)。

但是,這主要是針對那些應用程序被銷燬並重新創建的實例,因爲用戶更改了方向。

如果你希望數據是持久的(例如,你去那個活動,返回到調用活動,甚至可能完全切換到另一個應用程序,然後回來),我建議保存一切到你的SharedPreferences onPause( ),然後將它放回onResume()中。這樣,數據將保存在持久存儲器中,甚至超過系統重置時間。

+0

很酷,謝謝。我從來沒有看過SharedPreferences,所以這是我開始的地方。 – Will 2010-09-06 15:37:43

+0

好吧,一直在狩獵,我沒有做正確的事情。我認爲我無法保存活動狀態的原因是因爲用戶回到活動時調用的佈局。有沒有更好的方法來處理這個問題? – Will 2010-09-06 22:07:35

+0

你能更具體嗎?什麼不行? – EboMike 2010-09-06 23:50:54