• 5
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

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);
    }
      • 1
    • @ArunCThomas "Home as up button is to navigate to Home-Activity not previous acitivity" If other pakage app start Activity B and user press up button, must show Activity A (Because A is home of B).

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

  • 57
Reply Report
      • 1
    • I still can't get this to work as desired as the up navigation will recreated the parent activity with no saved state, is there something special about how you start your activities?
    • NavUtils.navigateUpFromSameTask(this) this results in NullPointerException if the Activity is being launched directly from some other application!
      • 2
    • I got the desired behavior solely by adding launchMode="singleTop" to the parent activity in the AndroidManifest.
      • 2
    • You don't typically need to provide an implementation for onOptionsItemSelected. To quote the official docs: "You do not need to catch the up action in the activity's onOptionsItemSelected() method. Instead, that method should call its superclass, as shown in Respond to Actions. The superclass method responds to the Up selection by navigating to the parent activity, as specified in the app manifest." developer.android.com/training/appbar/up-action.html
@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;

  • 32
Reply Report