1

I have two activities (A and B). Activity A calls activity B. Activity B has Back (Up) button like this:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Now when this UP button is pressed, onCreate of activity A is again called. In the activity A, there is a classId variable (which I got from an Intent) which I want to retain. For this I have following code in my onCreate of activity A:

        if (savedInstanceState == null)
        {
            Intent intent = getIntent();
            classId = intent.getIntExtra("CLASS_ID", 0);
        }
        else
        {
            classId = savedInstanceState.getInt("CLASS_ID");
        }

I have also overriden the onSavedInstanceState method:

@Override
protected void onSaveInstanceState(Bundle savedInstanceState)
{
    savedInstanceState.putInt("CLASS_ID", classId);
    super.onSaveInstanceState(savedInstanceState);
}

I am following this SO answer: onCreate being called on Activity A in up navigation

The problem I am facing is that when I come again to activity A by passing back button in activity B, onCreate gets called and I found savedInstanceState to be NULL.

Edit:

Is there any other way to save my classId variable so that when I return again to activity A, I can use that?

Edit 2

If I set launch mode of my activity A to SingleTop in the manifest file, my issue gets resolved. Is it the right approach?

Community
  • 1
  • 1
Daniel
  • 97
  • 8

3 Answers3

1

You should not suppose that onSaveInstanceState called each time you go to next activity.See the docs

This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state. For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored via onCreate(Bundle) or onRestoreInstanceState(Bundle).

You may further consult with official docs here

Junaid Hafeez
  • 1,618
  • 1
  • 16
  • 25
  • I have put a debug point in the onSavedInstanceState method and it is getting called when I am switching to activity B. But now when I am returning to activity A, the savedInstanceState bundle object is NULL. – Daniel Mar 29 '17 at 07:51
  • i am not sure but then perhaps you should call super.onSaveInstanceState(savedInstanceState); first – Junaid Hafeez Mar 29 '17 at 07:55
0

Try this

public class SingletonHolder {

    //your id here as what data type you want


    private  static SingletonHolder instance;

    public static SingletonHolder getInstance() {
        if (instance == null) {
            instance = new SingletonHolder();
        }
        return instance;
    }

   //set getter setter here
}

If not successfull feel free to comment

g7pro
  • 817
  • 1
  • 6
  • 11
  • Is this the standard way of saving variables while switching between the activities OR just a work around? – Daniel Mar 29 '17 at 10:18
  • Its a logic which we use actually , Its a concept its purely native and its java – g7pro Mar 29 '17 at 10:19
0

I changed the launchMode of the activity A to singleTop in the mainfest file like this:

android:launchMode="singleTop"

I followed this question on SO: Setting launchMode="singleTask" vs setting activity launchMode="singleTop"

By using this approach, activity A is not destroyed and when I just finish the activity B or click UP navigation in activity B, the existing instance of activity A is launched again.

Community
  • 1
  • 1
Daniel
  • 97
  • 8