Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

android how to finish an activity from other activity

In my app i have 3 activities.

From 1st activity it goes to 2nd and from 2nd it goes to 3rd. From 3rd it is coming to 1st again. and if I press back key from the 1st then it should go to home screen (App will stop). If I press back key of 1st its goes to 2nd activity again and if I press 2nd's back key, then it goes to the 1st . Then if I press back key of 1st then app stops.

What I want , when I am in 3rd activity and press the back button then it should go to 1st and simultaneously finish the 2nd activity.

How can I do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

just finish the second activity when you open third activity

suppose in second activity on some button click you are opening third activity using start activity;

startActivity(intent);
finish();//this will finish second activity and open third activity so when you press back from third activity it will open first activity.

if you want depended on some condition then on activity

setResult(123);

something code like this

now when override onActivityResult in second activity

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==123){
            //finish
        }
    }

also make sure one thing you need to use startActivityForResult(intent, requestCode); for result in second activity to start third activity.

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Activity2 extends Activity{

    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(new Intent(Activity2.this,Activity3.class)), 12);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if(resultCode==123 && requestCode==12){
            finish();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Activity3 extends Activity{

    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                setResult(123);
            }
        });
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...