简体   繁体   English

单击主页按钮时未调用onActivityResult

[英]onActivityResult wasn't called when click home button

I have three activity like this : 我有三个这样的活动:
+ LoginPage : display only one time when app installed + LoginPage:仅在安装应用程序时显示一次
+ MainActivity : Home screen of app + MainActivity:应用程序的主屏幕
+ TextNoteActivity: Sub activity is called by startActivityForResult from MainActivity + TextNoteActivity:子活动由MainActivity的startActivityForResult调用

I don't know why when i click up home button from TextNoteActivity. 我不知道为什么当我从TextNoteActivity中单击主页按钮时。 The app will close. 该应用程序将关闭。 The following is my code. 以下是我的代码。

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name="activity.MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar"
        >
    </activity>

    <activity
        android:name="activity.LoginPage"
        android:label="@string/app_name"
        android:noHistory="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

        </intent-filter>
    </activity>

    <activity android:name="activity.TextNoteActivity"
        android:parentActivityName="activity.MainActivity"
        >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="activity.MainActivity" />
    </activity>

    <activity
        android:name="activity.AddTagActivity"
        android:label="@string/title_activity_add_tag"
        android:theme="@style/AppTheme">

    </activity>

</application>

In MainActivity : 在MainActivity中:

Intent i = new Intent(this, TextNoteActivity.class);
startActivityForResult(i, REQUEST_ADDTEXTNOTE);

In TextNoteActivity : 在TextNoteActivity中:

    toolbar = (Toolbar) findViewById(R.id.addTextNote_toolbar);
    toolbar.setTitle("");
    setSupportActionBar(toolbar);

    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addTextNote();
            Toast.makeText(TextNoteActivity.this, "Back then", Toast.LENGTH_SHORT).show();
            finish();
        }
    });

Update 更新

   private void addTextNote() {
    String title = etTitle.getText().toString();
    String content = etContent.getText().toString();

    if (!title.isEmpty() &&  !content.isEmpty()) {
        TextNote textNote = new TextNote(title, 0, content);
        database.addTextNote(textNote, null);
        setResult(RESULT_OK);
    }
    else {
        setResult(Activity.RESULT_CANCELED);
    }

}

That is expected behavior 那是预期的行为
There is no reason for your activity to return a result when the user pushes the Home button. 用户按下“ Home按钮时,您的活动没有理由返回结果。
If you need to catch the Home button event 如果您需要捕捉“ Home按钮事件
You could override onPause() and finish() the activity. 您可以重写onPause()finish()活动。

DISCLAIMER 免责声明
But I'm afraid this is not a good app-behavior 但恐怕这不是一个很好的应用程序行为
Because the user won't expect the app state to change when he pushes the Home button. 因为用户在按下“ Home按钮时不会期望应用程序状态发生变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM