问题描述
我有一个包含抽屉的主活动,然后有其他活动在其顶部打开,并且它们还具有抽屉菜单(它们继承了主活动)
I have a main activity that contains a drawer and then i have other activities that opens on top of it they also have the drawer menu(they inherit main activity)
public class OBDActivity extends MainActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.activity_setting, null, false);
mDrawer.addView(contentView, 0);
getSupportActionBar().setTitle(R.string.OBDParams);
}
...
}
我要保持主要活动的生命,并在打开新活动时杀死所有其他活动吗?
I want to keep the main activity alive and kill all of the other activities when a new activity is being opened?
我已经尝试过了,但是它只是杀死了与正在打开的活动相同的旧活动.
I already tried this but it just kill the old activity that is the same activity that is being opened.
Intent intent=new Intent(this, MessagesActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
实际上,我想启动新的Activity并完成当前的Activity,无论它是相同的Activity或不同的类型.
In fact I want to start new Activity and finish current one either its the same activity or a different type.
推荐答案
我唯一的方法是自己关闭旧的activity
,因此我保留了正在打开并关闭它的activity
的引用.当一个新的要打开的时候.
The only way i could do this was to handle closing old activity
by myself so i kept a reference of the activity
that was opening and close it when a new one was going to open.
我在Main Activity
中创建了一个名为oldActivity
的变量,并在每个其他活动onCreate
方法中对其进行了设置:
I created a variable named oldActivity
in my Main Activity
and i set it in each and every other activities onCreate
method :
openedActivity = this;
然后每当要从抽屉菜单中打开另一个activity
时,我就完成了旧的菜单:
Then whenever another activity
was going to open from drawer menu i just finished the old one:
oldActivity.finish();
这篇关于如何在关闭所有其他Android设备的同时保持活动状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!