kAnNaN
kAnNaN

Reputation: 3679

Switching between activity?

In an given Android activity, I would like to start a new activity for the user at some point. Once they leave the first activity and arrive at the second, the first activity is hidden..

Now my question is

I want to bring back the first activity (i dont want to create a new instance of the first activity but to bring back the already existing instance of the first activity) when a button is clicked in the second activity ...

thanks :)

Upvotes: 1

Views: 291

Answers (4)

Sora
Sora

Reputation: 409

FirstActivity.java{

private static final int SECOND_ACTIVITY = 0;

openSecondActivity(){

Intent forChildIntent= new Intent( this ,FirstActivity. class );
//data for second activity forChildIntent.putExtra("userName", getUsrName());
this.startActivityForResult(forChildIntent, SECOND_ACTIVITY);

}

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

    case RESULT_OK:  //do something

            default:break;

}

}


SecondActivity.java{

goBackButtonClick(){ Intent retData=new Intent();

//set data to pass back ,if required //retData.putExtra("userName", getUsrName());

setResult(RESULT_OK, retData);

finish();//will take you to the first activity

}

}

Upvotes: 0

Rohit Mandiwal
Rohit Mandiwal

Reputation: 10462

so simple. integrate the below code in your second activity

Button b = (Button)findViewById(yourbuttonid here);
b.setOnClickListener(new View.onClickListener(){
    public void onClick(View v){
        finish();
    }
});

This will work

Upvotes: 2

WarrenFaith
WarrenFaith

Reputation: 57702

Depending on the usage of your second activity, you could also use startActivityForResult() when you start your second activity...

Upvotes: 1

Peter Knego
Peter Knego

Reputation: 80340

You would define the first activity with launchMode="singleInstance", then you would start the activity as usual.

Upvotes: 1

Related Questions