简体   繁体   English

接收者活动后返回上一屏幕

[英]Returning to previous screen after receiver activity

I have a app that has a activity that can be started from the start menu and a activity that is started by a broadcast receiver. 我有一个应用程序,该应用程序的活动可以从“开始”菜单启动,而活动可以由广播接收器启动。

在此处输入图片说明

Now on boot , if the receiver is called , the "event viewer" activity is started...after backing out of this activity user returns to previous screen (could be homescreen or whatever user was doing) .. this is how it should be. 现在启动时,如果调用了接收器,则将启动“事件查看器”活动...退出该活动后,用户将返回到先前的屏幕(可以是主屏幕或任何用户正在执行的操作)..这应该是这样的。

But if i start the "main" activity from the main launcher, and press the home button to go back to the home screen..the problem begins. 但是,如果我从主启动器启动“主”活动,然后按主屏幕按钮返回主屏幕。问题开始了。 Now if the receiver is called the "event viewer" activity is shown. 现在,如果将接收方称为“事件查看器”,则会显示活动。 if the user backs out of the "event viewer" activity (or i call finish() ) it will show the "main" activity(still running in background) instead of the previous thing the user was doing (like home screen). 如果用户退出“事件查看器”活动(或我称为finish()),它将显示“主要”活动(仍在后台运行),而不是用户之前所做的事情(如主屏幕)。

This is not how i want it..because it causes users after dismissing a calendar event (the purpose of my app) to return to eg the settings from the main app.. 这不是我想要的..因为它导致用户在取消日历事件(我的应用程序的目的)后返回到主应用程序的设置。

If i call finish() in onpause , it works ok...but that is not the way it should work. 如果我在onpause中调用finish(),它可以正常工作...但是那不是它应该工作的方式。

Any clues? 有什么线索吗?

Hope the problem is clear , since english isn't my first language i found it hard to explain the problem :-) 希望问题清楚,因为英语不是我的母语,所以我很难解释这个问题:-)

Thanks.. 谢谢..

In your receiver, use the flag FLAG_ACTIVITY_NEW_TASK 在您的接收器中,使用标志FLAG_ACTIVITY_NEW_TASK

@Override
public void onReceive(Context context, Intent intent) {
    //... bla bla bla

    Intent in = new Intent(context, IndependentActivity.class);
    in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(in);

    //... bla bla bla
}

In your manifest, provide a taskAffinity for your IndependentActivity. 在清单中,为您的IndependentActivity提供一个taskAffinity。

    <activity
        android:name=".IndependentActivity"
        android:excludeFromRecents="true"
        android:taskAffinity="com.example.independantactivty.ind" >
    </activity>

I have excluded it from the Recents but that is totally optional. 我已将其从“最近事件”中排除,但这完全是可选的。

It's generally unwise in my opinion to launch activities unless the user has elected to run the app (widget press, notification select, launcher, etc). 在我看来,除非用户选择运行该应用程序(小工具按,通知选择,启动器等),否则启动活动通常是不明智的。 Your example is just one reason why suddenly changing the activity when the user isn't expecting it might cause problems. 您的示例只是为什么当用户不期望突然更改活动可能导致问题时的原因之一。

If the user isn't using the app the best way to get their attention is through the status bar notification system. 如果用户不使用该应用程序,则引起注意的最佳方法是通过状态栏通知系统。

put

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1000) {
        if (resultCode == 1) {
            Main.this.finish();
        }

    }}

in your main activity and in your other activity, put this.setResult(1); 在您的主要活动和其他活动中,放置this.setResult(1); before call yourOtherActivity.this.finish() 在调用yourOtherActivity.this.finish()之前

I think it's that. 我想就是那样。 What's your first language? 你的母语是什么?

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

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