簡體   English   中英

在一個活動中按下手機后退按鈕會轉到另一個在 Android 之前尚未初始化的活動

[英]Pressing phone back button in one activity goes to another activity that hasn't been initialized before Android

這與 SO 給我的所有“類似”問題都非常不同。 我正在開發一個應用程序,當用戶在一個名為 SignUpActivity 的活動中注冊帳戶時,我使用以下代碼將他們帶到另一個名為 UserProfileActivity 的活動:

注冊活動:

Intent userProfileIntent = new Intent(SignUpActivity.this, UserProfileActivity.class);
startActivity(userProfileIntent);
finish();

現在,在 UserProfileActivity 中,如果我按下后退按鈕,它會將我帶到一個在應用程序的生命周期中從未被訪問/實例化的名為 PartyListActivity 的活動。

用戶配置文件活動:

public class UserProfileActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_profile);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

}

所以我的問題是,為什么當我在 UserProfileActivity 中按回時,它會回到 PartyListActivity,而我以前從未使用過 PartyListActivity? 清單文件中是否有規定此行為的內容?

編輯

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sample.package">

    <!-- To auto-complete the email text field in the login form with the user's emails -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <activity
            android:name=".LoginActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SignUpActivity"
            android:label="@string/title_activity_sign_up" />
        <activity
            android:name=".PartyListActivity"
            android:label="@string/title_activity_party_list"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".UserProfileActivity"
            android:label="@string/title_activity_user_profile"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".FriendsActivity"
            android:label="@string/title_activity_friends"
            android:theme="@style/AppTheme.NoActionBar" />
    </application>

</manifest>

我會盡力打破這些東西並遵循活動流程:

  • 您的啟動活動是LoginActivity
    (來自您的manifest文件)。

  • 然后,從LoginActivity您輸入到SignUpActivity ,無論是與一些UI操作或以編程(假設LoginActivity未完成)。

  • 從您輸入的SignUpActivityUserProfileActivity (完成SignUpActivity即從返回堆棧中刪除SignUpActivity )。

  • 現在您在UserProfileActivity ,然后從那里按后退按鈕LoginActivity您帶到LoginActivity因為SignUpActivity已從后退堆棧中刪除。

  • 但是你說,你在PartyListActivity ,只有當你從LoginActivity編程方式啟動它時LoginActivity

查看您的LoginActivity ,意外的活動從那里開始。

更多關於任務和返回堆棧

您可以在 AndroidManifest.xml 中的活動定義中指定“parentActivityName”屬性。 你的 SignUpActivity 的定義中有這樣的屬性嗎?

此屬性的完整文檔和有關返回堆棧導航的其他信息可在https://developer.android.com/training/implementing-navigation/temporal.html 獲得

選項 1:在 UserProfileActivity 完成后啟動 PartyListActivity 是沒有意義的。 您以某種方式配置或調用該活動的某個地方。 仔細檢查您的代碼。

選項 2:嘗試完成 UserProfileActivity 活動本身 onBackPress() (即使它會完成)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM