无法关闭android eclipse上的所有活动

时间:2012-07-11 10:02:36

标签: android eclipse

假设我有四个班级。每个班级都有一个按钮,可以从一个页面切换到另一个页面,工作正常。但是现在我试图通过点击最后一页的按钮关闭所有活动。我我也试过了,但它只关闭了最后一页。如何实现这个概念?

请查找我的代码以供参考

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>

感谢您的时间!..

4 个答案:

答案 0 :(得分:5)

试试吧。考虑一下Class3.java是您的上一个Activitiy而且,Button 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的{​​{1}}下面使用代码 -

onCreate()

希望这可以帮助您退出所有活动。

答案 1 :(得分:1)

我坚持用BroadCastReceiver创建一个BaseActivity,当你想要完成所有活动并使用这个BaseActivity扩展所有Activity类时,它会通知

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));

答案 2 :(得分:0)

你不应该这样做。如果要打开主屏幕或以前的应用程序屏幕。你只需致电

moveTaskToBack(boolean bool);

它会将您的应用发送到后台。或者如果你想在堆栈中没有class1 2和3,你可以在manifest decl的活动中使用excludeFromRecent标志。

针对同一问题,此主题获得了数千个观看次数。看一看并理解。

Is quitting an application frowned upon?

答案 3 :(得分:0)

在第3课中点击按钮时,请写下:

Intent intent=new Intent(Class3.this,Class1.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);