0

I am a begginer in android. I have a simple code, where I am calling one activity from my 1st activity and in that 2nd activity I have a button on press of which 2nd activity is finished and 1st activity comes up. Is there any way to execute the onSaveInstanceState in the 1st activity. Do I have to edit some thing in the manifest file?

Following is my Code

 public class Activity1 extends Activity {
  /** Called when the activity is first created. */

 TextView mTextView ;

 Button b1;
 static int count=0;

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



    mTextView = (TextView) findViewById(R.id.textView2);

    if (savedInstanceState == null) {
        mTextView.setText("Welcome to HelloAndroid!");
    } else {
        mTextView.setText("Welcome Back!");
        System.out.println("count------>"+ count);
    }



    final Intent i = new Intent(this,activity2.class);
    b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            startActivity(i);
            finish();
        }
    });

}


@Override
public void onResume() 
{
    super.onResume();
    System.out.println("inside Resume");
 }

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
      super.onSaveInstanceState(savedInstanceState);

      savedInstanceState.putBoolean("MyBoolean", true);
      savedInstanceState.putDouble("myDouble", 1.9);
      savedInstanceState.putInt("MyInt", 1);
      savedInstanceState.putString("MyString", "Welcome back to Android");
      count++;
    }


    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);

      boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
      double myDouble = savedInstanceState.getDouble("myDouble");

      System.out.println("MyBoolean"+ myBoolean);
      System.out.println("myDouble"+ myDouble);
    }


}

and here is my 2nd activity which is called by the 1st activiy

public class activity2 extends Activity{

TextView textview;
Button b1;
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);

        textview = (TextView) findViewById(R.id.textView1);

        textview.setText("in activity2");

             final Intent i = new Intent(this,Activity1.class);

        b1 = (Button) findViewById(R.id.button1);

        b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                startActivity(i);           
                finish();
            }
        });
 }
}

Some one please help me Thanks!

Atul Panda
  • 357
  • 5
  • 20
  • this may help you protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SharedPreferences mPrefs = getSharedPreferences(); mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE); } fro further info see http://developer.android.com/reference/android/app/Activity.html – Mudasar Sep 26 '12 at 06:47

2 Answers2

1

I believe what you're looking for is OnActivityResult

Override onActivityResult in activity 1 like this

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       //Call methods here that you want to execute when your activity is returned to
}

Then in activity 1, change how you start the activity 1 to

    startActivityForResult(i, 0)

If you want to pass any information back to activity 1 from the activity 2, you can create a bundle in activity two and set it with setResult() before you call finish()

    Intent resultIntent = new Intent();
    //put data in resultIntent
    setResult(RESULT_OK, resultIntent);
    finish()

Also, in activity 2, you don't need to directly start activity 1 again with startActivity() before you call finish. After you call finish() and it will automatically return you to the activity you came from. If you call startActivity it will actually create a new activity 1 on the navigation stack, which will be completely independent of the original activity 1 and all of its data.

EDIT:

Sorry, I had not noticed this before but make sure you do not call finish() in your first activity when you are starting the second. If you do, it will close the activity completely and you will lose all data you had in it (including the savedInstanceState). If you take that line out, this will work as expected.

In addition, it will let android automatically form a backstack for you, so hitting the back button on the phone will automatically go up your trail of calling activities instead of making you creating buttons manually to do so.

Gabriel
  • 2,279
  • 1
  • 20
  • 17
  • @Garbriel : If I do this way then my application is getting closed when I press on finish button in my second activity.. – Atul Panda Sep 27 '12 at 04:54
  • I updated my response, it should explain everything now. Let me know if there's anything else I missed. – Gabriel Sep 27 '12 at 06:05
0

Found another post where someone had a similar problem. You need to override the onSaveInstanceState method:

Saving Android Activity state using Save Instance State

Hope that helps.

Community
  • 1
  • 1
Kieren Hughes
  • 574
  • 3
  • 8