假设我有四个classes.Each类有一个按钮可以从一个页面切换到另一个页面,那么它的工作fine.But现在正在尝试通过单击上一页的按钮来关闭所有的活动。我也尝试过了,但是它只是关闭了最后一个page.How来实现这个概念?
请找我的代码作为参考
Xit_demoActivity.java
public class Xit_demoActivity extends Activity {
/** Called when the activity is first created. */
Button btn1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button)findViewById(R.id.button_1);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), Class1.class);
startActivity(myIntent);
}
});Class1.java
public class Class1 extends Activity
{
Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
btn1 = (Button)findViewById(R.id.button_2);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), Class2.class);
startActivity(myIntent);
}
});Class2.java
public class Class2 extends Activity
{
Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
btn1 = (Button)findViewById(R.id.button_3);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), Class3.class);
startActivity(myIntent);
}
});Class3.java
public class Class3 extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
Button next = (Button) findViewById(R.id.button_4);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.and.roidu.xit"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Xit_demoActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity><activity android:name=".Class1"></activity>
<activity
android:name=".Class1"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN1" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity><activity android:name=".Class2"></activity>
<activity
android:name=".Class2"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN2" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity><activity android:name=".Class3"></activity>
<activity
android:name=".Class3"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN3" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>谢谢你抽出时间!.
发布于 2012-07-11 10:06:31
试试看这个。假设您的Class3.java是您的最后一个Activitiy,在Button of next中使用以下代码-
Intent intent = new Intent(currentActivity.this, XitActivity.class); // instead of XitActivity use your first activity
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);而且,在你的Xit_demoActivity的onCreate()下面使用代码-
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}希望这将帮助你退出所有的活动。
发布于 2012-07-11 10:19:57
我坚持使用一个BaseActivity创建一个BroadCastReceiver,当您想要完成所有活动并使用这个BaseActivity扩展所有活动类时,这个BroadCastReceiver会通知您,
public static final String ACTION_FINISH = "ACTION_FINISH";
public class BaseActivity extends Activity {
public void onCreate(Bundle bundle, int resourceId) {
super.onCreate(bundle);
registerReceiver(finishActivitiesReceiver,
new IntentFilter(ACTION_FINISH));
}
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(finishActivitiesReceiver);
}
BroadcastReceiver finishActivitiesReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
}然后,当您想要关闭所有活动时,只需发送BroadCast。
sendBroadcast(new Intent(BaseActivity.ACTION_FINISH));发布于 2012-07-11 10:09:15
你不应该这么做。如果您想打开主屏幕或以前的应用程序屏幕。你只需打电话
moveTaskToBack(boolean bool);它会将你的应用程序发送到后台。或者,如果您想在堆栈中不包含class1 2和3,则可以在活动的清单标记中使用excludeFromRecent标志。
这个线程为同样的问题获得了数千个视图。看一看,了解一下.
https://stackoverflow.com/questions/11430184
复制相似问题