简体   繁体   English

从Activity而不是父级返回主屏幕

[英]Returning to home screen from Activity instead of parent

I have a music player activity which doesn't behave as I want. 我的音乐播放器活动不符合我的要求。 This activity can be opened from inside the app, from notifications bar and when switching/resuming app from background. 此活动可以从应用程序内部,通知栏以及从后台切换/恢复应用程序时打开。

When it is launched from the app -> backPress on activity -> returns to previous app activity. 当它从应用程序启动时 - > backPress on activity - >返回到之前的应用程序活动。 OK

When launched from notification -> backPress on activity -> returns to home screen (it is OK) 从通知启动时 - > backPress on activity - >返回主屏幕(可以)

When resumed from homeScreen/recent apps -> backPress on activity -> returns to home screen (not OK) - user assume to get back to the app since this activity is a leaf and a tab activity is the root activity. When resumed from homeScreen/recent apps -> backPress on activity -> returns to home screen (not OK) - 用户假设回到应用程序,因为此活动是叶子,而tab活动是根活动。

I want to get back to the parent activity when pressing back, not going on the home screen(when resumed from notifications, it is OK if returns to home screen, but both variants are OK for me in this scenario) 我想在回按时回到父活动,而不是在主屏幕上(当从通知恢复时,如果返回到主屏幕则可以,但在这种情况下两种变体都可以)

<activity android:name=".player.PlayerActivity"
                    android:configChanges="keyboardHidden|orientation"
                    android:label="@string/audio_player_activity_title"
                    android:launchMode="singleInstance"/>

and starting the activity: 并开始活动:

//this intent is started from a fragment (SherlockFragment)
Intent i = new Intent(getActivity(), PlayerActivity.class);
startActivity(i);

NOTE: I want single instance to avoid 2 activities of the same type running on screen (happen when using notifications) 注意:我希望单个实例避免在屏幕上运行相同类型的2个活动(在使用通知时发生)

Could someone help me with this? 有人可以帮我吗?

When you use the launchMode: "singleInstance", your activity will be launched in a new Task: [http://developer.android.com/guide/topics/manifest/activity-element.html][1] That's why you dont see the normal behavior of back stack. 当您使用launchMode:“singleInstance”时,您的活动将在新的任务中启动:[http://developer.android.com/guide/topics/manifest/activity-element.html] [1]这就是为什么你不这样做看到后栈的正常行为。 To be sure that android instanciates once your activity you can set the flag : FLAG_ACTIVITY_REORDER_TO_FRONT when you launch your activity 为了确保android一旦你的活动实例化,你可以设置标志:FLAG_ACTIVITY_REORDER_TO_FRONT当你启动你的活动

intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

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

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