androidbuttonback-buttonhome-buttonup-button

What differs Up Button to Back Button? And how to make Up Button work like Back Button?


In terms of how each button treat the activities? What are the differences?

I have 3 activities (let's call them A, B, and C). A is B's parent and B is C's parent, and one intent take a extra to the the next activity. I go from A to B and then B to C. When I try to go back using Up Button, the app crashes, and that's because it tries to get a info from the extra in the intent.

But when I use Back Button, it works (???) and I don't know why.

I tried to make Up Button work like Back Button, doing the following:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.home:
            onBackPressed();
            break;
    }
    return super.onOptionsItemSelected(item);
}

and also tried using NavUtils.navigateUpFromSameTask(this) but it's not working either. How can I solve it?

Edit: error Log

2019-01-09 03:45:43.468 29326-29326/com.jvponte.maosdadasv1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jvponte.maosdadasv1, PID: 29326
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jvponte.maosdadasv1/com.jvponte.maosdadasv1.UserActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.jvponte.maosdadasv1.User.getUsername()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:169)
    at android.app.ActivityThread.main(ActivityThread.java:6568)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.jvponte.maosdadasv1.User.getUsername()' on a null object reference
    at com.jvponte.maosdadasv1.UserActivity.onCreate(UserActivity.java:67)
    at android.app.Activity.performCreate(Activity.java:7016)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:169) 
    at android.app.ActivityThread.main(ActivityThread.java:6568) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

Edit: some more code I have this in onCreate() on my activity B (UserActivity). The intent extra is added on activity A

    Intent intent = getIntent();

    if(intent.hasExtra("clickedUser")){
        mOtherUser = intent.getParcelableExtra("clickedUser");
    }
    if(intent.hasExtra("loggedUser")){
        mLoggedUser = intent.getParcelableExtra("loggedUser");
    }

    /.../

    mUsernameTV.setText(mOtherUser.getUsername());
    mUserInfoTV.setText(mOtherUser.getUserInfo());

Here I call activity C (ChatActivity)

Intent intent = new Intent(UserActivity.this, ChatActivity.class);
intent.putExtra("loggedUser", mLoggedUser);
intent.putExtra("clickedUser", mOtherUser);
startActivityForResult(intent, RC_CHAT);

In ChatActivity onCreate() I have this

    Intent intent = getIntent();
    if (intent.hasExtra("clickedUser")){
        otherUser = intent.getParcelableExtra("clickedUser");
        otherUserId = otherUser.getUid();
        otherUserName = otherUser.getUsername();
    }
    if (intent.hasExtra("loggedUser")){
        loggedUser = intent.getParcelableExtra("loggedUser");
        loggedUserId = loggedUser.getUid();
    }

The error is problably this loggedUser and otherUser extra not being correctly managed. The weirdest thing is that using Back Button WORKS, while setting Up Button to work like Back Button doesn't


Solution

  • I just removed the parent relation between B and C and used the code onOptionSelected() that I posted and it worked. Even when using that, making B C's parent was making it crash.

    I still don't know why using Back Button works and Up Button doesn't, but it fixed the problem. Thanks everyone who tried to help