简体   繁体   English

按下时,Android Intent FLAG_ACTIVITY_TASK_ON_HOME不会返回首页

[英]Android Intent FLAG_ACTIVITY_TASK_ON_HOME doesn't return to home when pressing back

I have a Chat app with this situation: 我有一个与这种情况的聊天应用程序:

A - MainActivity A-MainActivity

B - Chat activity B-聊天活动

C - Forward activity C-前进活动

Let's assume i want to forward a message from Bill to John's chat, i open A -> B -> C, on C i must select the user to forward, so i click on John throwing a new B intent, now in the stack i have ABCB, if from John's chat i press the back button i want to go back in A (MainActivity), but actually i go back in B (Bill's chat), this is a problem because the Chat activity must be only one. 假设我想将消息从Bill转发到John的聊天室,我打开A-> B-> C,在C上我必须选择要转发的用户,所以我单击John抛出一个新的B意图,现在在堆栈中有ABCB,如果从John的聊天中我按“后退”按钮,我想回到A(MainActivity),但实际上我又回到B(Bill的聊天),这是一个问题,因为Chat活动必须只有一个。 For necessity the Chat activity is set as android:launchMode="singleTop" in the manifest, because if i have a Chat in foreground and i click on "new message" android notification, the current Chat is closed and the new one is opened. 必要时,在清单中将Chat活动设置为android:launchMode =“ singleTop”,因为如果我在前台有一个Chat并单击“新消息” android通知,则会关闭当前的Chat并打开新的Chat。 Reading on developers guide i've found FLAG_ACTIVITY_TASK_ON_HOME, as i can understand with this flag i should be able to back directly from John's chat(B) to MainActivity(A) but doesn't happens. 通过阅读开发人员指南,我发现FLAG_ACTIVITY_TASK_ON_HOME,正如我可以理解的那样,使用该标志我应该能够直接从John的chat(B)返回到MainActivity(A),但不会发生。

This is A (MainActivity with sliding tab layout, the Intent is launched from ListFragment Tab activity) 这是A(具有滑动选项卡布局的MainActivity,从ListFragment Tab活动启动Intent)

    Intent intent = new Intent(getContext(), Chats.class);
    Bundle b = new Bundle();
    b.putString("nid", list_id.toString()); 
    b.putString("user_id_key", list_user_id.toString());
    b.putString("name", list_name.toString());
    b.putString("number", list_number.toString());
    intent.putExtras(b); 
    startActivityForResult(intent, 101);

This is B (Chat activity - AppCompatActivity) 这是B(聊天活动-AppCompatActivity)

    Intent intent = new Intent(getApplicationContext(), Forward.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    Bundle b = new Bundle();
    b.putString("forward_mess", MessText);
    b.putString("number", number);
    b.putString("conv_id", convId);
    intent.putExtras(b);
    startActivity(intent);

This is C (Forward activity - AppCompatActivity) 这是C(转发活动-AppCompatActivity)

    Intent intent = new Intent(getApplicationContext(), Chats.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_TASK_ON_HOME);
    Bundle b = new Bundle();
    b.putString("nid", sel_nid);
    b.putString("user_id_key", sel_user_receiver);
    b.putString("name", sel_name);
    b.putString("number", sel_number);
    b.putString("forward_mess", forward_mess);
    intent.putExtras(b);
    startActivityForResult(intent, 101);
    finish();

Also tried with FLAG_ACTIVITY_CLEAR_TOP, but this brings me directly to A->B 还尝试了FLAG_ACTIVITY_CLEAR_TOP,但这将我直接带到A-> B

Solved using onActivityResult method in Chat activity. 在聊天活动中使用onActivityResult方法解决。

Here the correct activities flow: 这里是正确的活动流:

MainActivity -> startActivityForResult(chat_intent, 101); MainActivity- > startActivityForResult(chat_intent,101);

Chat -> startActivityForResult(forward_intent, 5); 聊天 -> startActivityForResult(forward_intent,5);

Set the onActivityResult method: 设置onActivityResult方法:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //If is 5 means Forward activity was closed
    if (requestCode == 5) {
        // If 10 means a new Chat was opened, so this one must be closed
        if (resultCode == 10) {
            finish();
        }
    }
}

Forward 向前

startActivityForResult(chat_intent, 101);
setResult(10);
finish();

暂无
暂无

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

相关问题 Intent.FLAG_ACTIVITY_CLEAR_TASK与Intent.FLAG_ACTIVITY_TASK_ON_HOME之间的区别 - Difference between Intent.FLAG_ACTIVITY_CLEAR_TASK and Intent.FLAG_ACTIVITY_TASK_ON_HOME 意图启动“活动”,但是按主屏幕按钮时“活动”消失 - Intent launches Activity, but Activity disappears when pressing the home button Android活动-按下首页后返回无法正常工作 - Android activity - Back not working properly after pressing home 即使按下主屏幕按钮或后退按钮,Android应用程序也会重新打开并继续进行下一个活动 - Android app reopening and proceeding on the next activity even when pressing the home button or back button Flutter - 按返回按钮到 android 主屏幕时丢失 state - Flutter - losing state when pressing back button to android home screen android:按下单个任务的HOme键的行为 - android : behaviour on pressing HOme key for single task 在按下Android设备主屏幕按钮后,启动活动需要哪些Intent标志 - Android which intent flag required for start activity after android device home button press Android:返回Application Home Activity - Android: return to the Application Home Activity Android家庭活动要进行不同系列的活动然后再回到家庭活动吗? - Android home activity to different series of activities and then back to home activity? 按下首页时如何处理android中的多个活动任务? - How to handle multiple activity task in android when press home?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM