无法关闭所有活动的android日食日食、android

2023-09-12 09:58:03 作者:紾惜、這簖情

假设我有四个classes.Each类是有一个按钮,可以切换画面从一个网页到另一个页面,以及它的工作fine.But现在我试着从最后page.I点击按钮关闭所有活动已经尝试过了,但它只是关闭只剩下最后page.How实现这个概念?

Assume i have four classes.Each class is having a button to do screen switching from one page to another page,well its working fine.But now im trying to close all the activities by clicking the button from the last page.I have tried that too, but its just closing only the last page.How to achieve this concept?

请找我的code仅供参考

Please find my code for reference

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>

感谢您的时间!..

Thanks for your time!..

推荐答案

简单的尝试。考虑你的 Class3.java 是你最后的 Activitiy 键,在按钮接下来使用下面code -

Simply try this. Consider your Class3.java is your last Activitiy And, in Button of next use below code -

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()使用下面code -

And, in your Xit_demoActivity's onCreate() use below code -

if (getIntent().getBooleanExtra("EXIT", false)) {
    finish();
}

希望这将帮助您从所有活动离开你。

Hope this will helps you to exit you from all activities.