Java 奇怪的行为:首次启动FLAG\u Activity\u NEW\u任务时,活动没有重新启动
我的设想是 当应用程序启动时,在用户登录后,我将以以下方式启动一项活动Java 奇怪的行为:首次启动FLAG\u Activity\u NEW\u任务时,活动没有重新启动,java,android,android-intent,android-activity,Java,Android,Android Intent,Android Activity,我的设想是 当应用程序启动时,在用户登录后,我将以以下方式启动一项活动 Intent rangeFinderScreen = new Intent(YourLocationScreen.this, RangeFinderScreen.class); rangeFinderScreen.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(rangeFinderScreen)
Intent rangeFinderScreen = new Intent(YourLocationScreen.this, RangeFinderScreen.class);
rangeFinderScreen.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(rangeFinderScreen);
我正在使用Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK
,以便清除所有以前的活动堆栈,并将其作为新任务启动
然后我有一个底部栏在活动之间导航,所以我创建了一个公共侦听器(活动),并在所有活动中实现了它。看起来是这样的,
public class CommonListenerForBottomButtons extends Activity implements View.OnClickListener{
Context context;
String buttonName;
public CommonListenerForBottomButtons(Context context, String buttonName){
this.context = context;
this.buttonName = buttonName;
}
@Override
public void onClick(View v) {
switch(buttonName){
case "play":
Intent rangefinder = new Intent(context, RangeFinderScreen.class);
rangefinder.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(rangefinder);
break;
// and so on more cases
}
}
}
Intent rangeFinderScreen = new Intent(YourLocationScreen.this, RangeFinderScreen.class);
startActivity(rangeFinderScreen);
担忧
如果在第一次开始活动时,我执行以下操作
public class CommonListenerForBottomButtons extends Activity implements View.OnClickListener{
Context context;
String buttonName;
public CommonListenerForBottomButtons(Context context, String buttonName){
this.context = context;
this.buttonName = buttonName;
}
@Override
public void onClick(View v) {
switch(buttonName){
case "play":
Intent rangefinder = new Intent(context, RangeFinderScreen.class);
rangefinder.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(rangefinder);
break;
// and so on more cases
}
}
}
Intent rangeFinderScreen = new Intent(YourLocationScreen.this, RangeFinderScreen.class);
startActivity(rangeFinderScreen);
然后所有的工作都可以
但是,如果我按照以下方式(如我现在所做的),即使用rangeFinderScreen.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)代码>
然后活动不再开始
但是,如果我从任何其他活动开始活动,而不是像这样的普通侦听器活动
Intent rangefinder = new Intent(MyGolflerScreen.this, RangeFinderScreen.class);
startActivity(rangefinder);
然后它启动
编辑
以下是我在所有活动中初始化公共侦听器的方式:
homeLinearLayout.setOnClickListener(new CommonListenerForBottomButtons(getApplicationContext(), "home"));
playLinearLayout.setOnClickListener(new CommonListenerForBottomButtons(getApplicationContext(), "play"));
weatherLinearLayout.setOnClickListener(new CommonListenerForBottomButtons(getApplicationContext(), "weather"));
messageLinearLayout.setOnClickListener(new CommonListenerForBottomButtons(getApplicationContext(), "message"));
myGolflerLinearLayout.setOnClickListener(new CommonListenerForBottomButtons(getApplicationContext(), "mygolfer"));
除最初使用标志Intent.flag\u ACTIVITY\u NEW\u TASK
启动的活动外,所有其他活动都将启动
清单文件条目
<activity
android:name=".RangeFinderScreen"
android:label="@string/title_activity_range_finder_screen"
android:screenOrientation="portrait" >
</activity>
从应用程序中的另一个活动启动活动时,不要设置标志意图。标记活动\u新任务。这是没有必要的,可能是造成你的问题
此外,如果每次用户单击按钮时您都要启动一个新的活动
,那么您会发现每次都在创建这些活动的新实例,并且过一段时间后会得到一大堆活动。这可能也不是人们想要的行为。要在任务堆栈中可能已经存在的活动之间切换,您可以设置标志Intent。将标志\u ACTIVITY\u REORDER\u设置为\u FRONT
如果您关心的是管理所有堆栈,我更喜欢使用所有“单顶”活动,并使用标志Intent启动它们。标志\u ACTIVITY\u CLEAR\u TOP
它将管理所有你想做的事情
您可以在活动标记内的清单中添加launchMode
,该标记等于“singleTop”
希望它能工作…你是说在rangefinderscreen类\@koutuk No的反按上,在底部导航栏按钮上单击该按钮,该按钮具有公共侦听器。在第二次使用导航选项卡时是否获得有效的上下文time@koutuk对如果可能的话,这是正确的。请在初始化公共列表的地方发布代码。。。。当我们绑定2-3个片段并进行一些活动导航时,wid片段也会发生同样的事情…我自己说这个问题是由意图引起的。标记活动新任务并且由于某些要求,我必须清除所有以前的活动任务,所以我需要同时使用它们Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK
如果需要设置Intent.FLAG_ACTIVITY_CLEAR_TASK
,那么还需要设置Intent.FLAG_ACTIVITY_NEW_TASK
。但是,在侦听器中的switch
语句中,您只设置了Intent.FLAG\u ACTIVITY\u NEW\u TASK
,这可能会导致您的问题。