Android onPause()或savedInstanceState()?

时间:2010-09-05 21:22:26

标签: android

我一直在研究我的应用程序,并且我需要了解Activity生命周期,我认为我没有得到它。我所拥有的是一个带有七个按钮的活动。当用户按下按钮时,它会打开一个listActivity,用户可以在其中选择列表中的项目,标题将被发送回调用的Activity,并且调用活动会在按钮上的文本旁边添加标题。 (它使用的方法是在switch语句中,它取决于请求代码返回给它 - 我认为它对我的目的来说有点弱。)但是如果用户离开活动并返回到它,通过后退按钮或任何其他按钮,当他们返回时没有任何额外信息 - 剩下的就是带有原始文本的七个按钮。我想留下这些信息 - 我该怎么做?

以下是调用Activity:

的代码
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)));

        }
    }




}

1 个答案:

答案 0 :(得分:1)

取决于。通常,您需要覆盖onSaveInstanceState()并将所有数据保存到Bundle(它是一个简单的键/值对事件)并在onCreate()(或onRestoreInstanceState()中重新创建它,如果您想要分离代码出)。

但是,这主要是因为用户更改了方向而销毁和重新创建应用的情况。

如果你想要数据是持久的(即你去那个活动,回到调用活动,甚至可能完全切换到另一个应用程序,然后再回来),我建议将所有内容保存到onPause()中的SharedPreferences中然后在onResume()中将其放回原位。这样,数据将处于持久存储状态,甚至比系统重置更长。