android:launchMode="singleTop"
2010-12-06 15:14
645 查看
Android: new Intent() starts new instance with
android:launchMode="singleTop"
I have Activity A
with
android:launchMode="singleTop"
in the manifest.
If I go to Activity
B
,
C
and
D
there I have menu shortcuts to return to my applications root activity (
A
).
The code looks like this:
Intent myIntent = new Intent(getBaseContext(), MainActivity.class);
startActivity(myIntent);
However, instead of returning to the already existing instance A of my MainActivity.class
it creates a new instance -> it goes to onCreate() instead of onNewIntent().
This is not the expected behavior, right?
This question and answers originated from stackoverflow.com
Question by Stefan Klumpp
Answer3:
This should do the trick.<activity ... android:launchMode="singleTop" />
When you create an intent to start the app use:
Intent intent=new Intent(context, YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
This is that should be needed.
Answer by PHP_Jedi
Answer2:
The "standard" and "singleTop" modesdiffer from each other in just one
respect: Every time there's new intent
for a "standard" activity, a new
instance of the class is created to
respond to that intent. Each instance
handles a single intent. Similarly, a
new instance of a "singleTop" activity
may also be created to handle a new
intent. However, if the target task
already has an existing instance of
the activity at the top of its
stack
, that instance will receive
the new intent (in an onNewIntent()
call); a new instance is not created.
I'm not 100% sure what "already has an existing instance of the
activity at the top of its stack" means, but perhaps your activity isn't
meeting this condition.
Would
singleTask
or
singleInstance
work for
you? Or perhaps you could try setting
FLAG_ACTIVITY_SINGLE_TOP
on the intent you are creating to see if that makes a difference,
although I don't think it will.
Answer by mbaird
It means that the task stack (application stack) that is currently active has an instance of that activity on top (e.g. an activity is starting another copy of the same activity when it has focus). Other instances of the same activity may exist on other tasks (applications). – Seth Illgard May 4 at 21:39
So if my will is to launch a new instance of activity in the same proccess, but i already FOCUSED on a previouse instance of that activity, how would i do that? – rayman Jul 14 at 7:31
Answer1:
What actually worked for me in the end was this:Intent myIntent = new Intent(getBaseContext(), MainActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
Answer by znq
original from: http://stackoverflow.com/questions/2424488/android-new-intent-starts-new-instance-with-androidlaunchmodesingletop
相关文章推荐
- android:launchMode="singleTop"
- android Activity android:launchMode="singleInstance" 与 startActivityForResult
- android:launchMode="singleTask" intent获取到的值没有更新
- 监听home键(android:launchMode="singleTask" 与 onNewIntent(Intent intent) 的用法)
- android:launchMode="singleTask" 与 onNewIntent(Intent intent) 的用法
- android:launchMode="singleTask" 与 onNewIntent(Intent intent) 的用法
- Activity设置启动模式android:launchMode="singleInstance" 按home键后会销毁
- android:launchMode="singleTask" 与 onNewIntent(Intent intent) 的用法
- 一个由Activity launchMode="singleTask"属性引起的血案(Android)
- android:launchMode="singleTask" 与 onNewIntent(Intent intent) 的用法
- android Task Activity launchMode="singleTask" singleInstance 代码分析
- 监听home键(android:launchMode="singleTask" 与 onNewIntent(Intent intent) 的用法)
- 理解Android的Activity launchMode:standard,singleTop,singleTask,singleInstance
- Android 监听home键(android:launchMode="singleTask" 与 onNewIntent(Intent intent) 的用法
- Activity launchMode="SingleTaks"的秘密
- Android 监听home键(android:launchMode="singleTask" 与 onNewIntent(Intent intent) 的用法
- Understand Android Activity's launchMode: standard, singleTop, singleTask and singleInstance
- 关于"singleTask" lauch mode,Android官方文档没有告诉你的事儿
- Android Activity launchMode singleTask/singleTop/singleInstance/standard 讲解
- android:launchMode="singleTask" 与 onNewIntent(Intent intent) 的用法