Jeremy
Jeremy

Reputation: 2536

Android Back Action Button Result in App Crash

I am building a new application which composed of 3 activities namely: - Splash Screen - Activity A - Activity B, and - Activity C

From activity A user can go to both activities indicated below:

A -> B -> C (from act A user can go to B then to C). A -> C (from act A user can go straight to C). B -> C (from B user can go to C).

i also pass Serializable intent Extra between activities.

The problem that i am having is whenever i pressed the back button on the Action Bar (Top Left Corner) it always makes my app crashes (Error: NULL Pointer Exception).

I have tried to place this code on ALL of my activity.

@Override
public void onBackPressed() {
    this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
}

I tried to somehow mimic the physical backbutton behaviour since it is working when user press physical backbutton. But somewhat throws error as well.

or

public void onBackPressed(){
  super.onBackPressed();
}

or (well this one literally restart the app, which is discourage since it restart the app from splash screen).

public void onBackPressed(){
  super.onBackPressed();
  finish();
}

Does anyone know the appropriate way to implement back button?

Upvotes: 0

Views: 6751

Answers (4)

Gaurav Setia
Gaurav Setia

Reputation: 294

If You are using API level 16 or more than , Really No need to do anythinngggg, i just Find the solution : in manifest file with every other activity Except MainActivity Add meta-data like this :

        android:name="android.support.PARENT_ACTIVITY"

        android:value="com.gsetia.ohama.MainActivity"/>

with in Activity like this , For Example

         <activity
                      android:name=".ViewReports"
                      android:label="@string/title_activity_view_reports"
                      android:parentActivityName=".MainActivity"
                      android:theme="@style/AppTheme" >

                     <meta-data
                     android:name="android.support.PARENT_ACTIVITY"
                     android:value="com.gsetia.ohama.MainActivity"/>

    </activity>

And In Last , delete public boolean onOptionsItemSelected(MenuItem item) Method. That's it.

You will find your Back Button and will work fine

Upvotes: 1

setchebehere
setchebehere

Reputation: 111

The button on the top left corner is not the "Back" button, it would be the "up" button, and it's just a button in the action bar, onBackPressed refers to the hardware back button being pressed. Navigation with back and up is not necessarily the same ("back" means go to where I was before, whereas "up" means go to the upper level in the app hierarchy). Take a look at http://developer.android.com/design/patterns/navigation.html for more information.

(Also, try to avoid a splash screen, they are highly discouraged in android design patterns)

Edit: I forgot to mention how to actually handle the "up" button. You do that on your activity's onOptionsItemSelected:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        // Handle "up" button behavior here.
        return true;
    } else {
        // handle other items here
    }
    // return true if you handled the button click, otherwise return false.
}

Upvotes: 7

Machee
Machee

Reputation: 11

I think, the problems is from passing value using intent. If the value is intended to be use by all of your activity, I think the best way is to use a sharedpreferences.

Upvotes: 1

Simon
Simon

Reputation: 6363

You can do something like this when the back button is called

public void onBackPressed(){
    // Add data to your intent
    finish();
    startActivity(intent);
}

And similar in your onClick method for your action bar button.

Upvotes: 1

Related Questions