向上按钮与向后按钮有何不同?以及如何使向上按钮像后退按钮一样工作?

时间:2019-01-09 06:22:25

标签: android button back-button home-button up-button

关于每个按钮如何处理活动?有什么区别?

我有3个活动(我们叫它们A,B和C)。 A是B的父级,B是C的父级,一个意图会给下一个活动带来额外的影响。我从A转到B,然后从B转到C。当我尝试使用“向上按钮”返回时,该应用程序崩溃了,这是因为它试图从意图中获取更多信息。

但是当我使用“后退按钮”时,它可以正常工作(???),我不知道为什么。

我尝试使“向上按钮”像“后退按钮”那样工作,执行以下操作:

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

,并且还尝试使用NavUtils.navigateUpFromSameTask(this),但是它也不起作用。我该怎么解决?

编辑:错误日志

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) 

修改:更多代码 我在活动B(onCreate())的UserActivity中有此文件。额外的意图添加到活动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());

在这里我叫活动C(ChatActivity

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

ChatActivity onCreate()中,我有这个

    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();
    }

错误很可能是此日志用户和其他用户未得到正确管理。最奇怪的是使用“后退按钮”工作,而将“向上按钮”设置为像“后退按钮”一样工作

3 个答案:

答案 0 :(得分:0)

尝试像下面那样更改代码,以模拟后退按钮行为:

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

答案 1 :(得分:0)

定期使用该应用程序时,看不见上下之间的主要区别。

但是例如,如果您从链接中打开一个明细活动,则该应用程序仅打开该明细活动。现在,如果按工具栏上的“后退”按钮,则会发生以下情况:

  • 导航表示您正在层次结构中向上移动,因此转到详细信息活动的父级,例如家庭活动。
  • 后退导航意味着,您将返回到之前的位置,这意味着将返回到上次使用的应用程序,例如您从中打开链接的浏览器。

如何执行此操作取决于您,因此您必须决定什么对您的应用程序用户来说更合乎逻辑或更有用。

  

,还尝试使用NavUtils.navigateUpFromSameTask(this),但是   也不工作。我该怎么解决?

这可能是因为您尚未为当前所在的活动定义父活动。

看看the tutorial(尤其是Specify Parent部分),其中详细介绍了向上导航的所有内容。


提供堆栈跟踪后进行编辑:

向上导航对您有效,只有您要导航的活动在其onCreate(...)方法中期望某种类型的用户。您必须确保将所有数据提供给父活动。

如果您通过意图提供数据,则可以使用以下代码打开父级:

Intent upIntent = NavUtils.getParentActivityIntent(this);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
    TaskStackBuilder.create(this)
        .addNextIntentWithParentStack(upIntent)
        .startActivities();
} else {
    NavUtils.navigateUpTo(this, upIntent);
}

答案 2 :(得分:0)

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