Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using actionbar home as up button, home activity saveInstanceState is always null

Activity A ===click button===> Activity B

When press back button, Activity A is not recreated.

When press home as up button, Activity A is recreated.

So I save state when A.onSaveInstanceState(Bundle outState) , and use state when A.onRestoreInstanceState(Bundle savedInstanceState).

Saving and Using works fine (except home as up button)

.

However,

When pressed home as up button, system recreate Activity A, and savedInstanceState is gone.

How can I use Saved Instance State?

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
        // I do not want this... 
        // Home as up button is to navigate to Home-Activity not previous acitivity
            super.onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
like image 741
ChangUZ Avatar asked May 08 '13 06:05

ChangUZ


People also ask

Why is savedInstanceState null?

savedInstanceState will always remain null after the app is killed. It is not passed on to the bundle. public void onCreate(Bundle savedInstanceState) { super. onCreate(savedInstanceState); if (savedInstanceState !=

Where the action bar is present in the activity screen by default?

In Android applications, ActionBar is the element present at the top of the activity screen.

How do I add action to action bar?

All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.


2 Answers

In the onCreate() enable the home button.

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
}

In the onOptionItemSelected() method do this.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

This should enable Up navigation. If you want the parent activity to be restored with a savedInstanceState. You should set launchMode="singleTop" in the parent activity in Manifest file.

For more info check out http://developer.android.com/: Providing Up Navigation

like image 179
Aegis Avatar answered Oct 23 '22 17:10

Aegis


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        finish();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

I used finish() insteed of NavUtils;

like image 40
Asrin Avatar answered Oct 23 '22 19:10

Asrin