3

In AActivity, when button1 is press, then call BActivity.

Button b1= (Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(AActivity.this, BActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});

In BActivity, I want that when button2 is press, then recall AActivity.

Button b2= (Button)findViewById(R.id.button2);
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//How to do?
}
});

I want back without press "back" button on keyboard. And replace with button in layout. How should I do?

When call back to AActivity, is it possible to run the onCreate() method?

brian
  • 6,802
  • 29
  • 83
  • 124

6 Answers6

7

Only you have to finish the activity by calling finish() method

Sample code

Button b2= (Button)findViewById(R.id.button2);
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
3

maybe just call finish() in your activity.

Natali
  • 2,934
  • 4
  • 39
  • 53
3

You'll get the "Back" button functionality by this simple code :) As the back button on the phone is just finishing the current activity causing previous activity on the activity stack to start again.

Button b2= (Button)findViewById(R.id.button2);
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
ArVan
  • 4,225
  • 8
  • 36
  • 58
2

In the onClick() method of button2, you could just put

finish();

or you could specify a new Intent with AActivity.class as the target.

iaindownie
  • 1,046
  • 12
  • 28
1
Button b2 = (Button) findViewById(R.id.button2);
b2.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setClass(BActivity.this, AActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }
});
jensgram
  • 31,109
  • 6
  • 81
  • 98
Ramindu Weeraman
  • 344
  • 2
  • 10
0

I think you should override onKeyDown() method to enable the back button functionality. Override back button to act like home button link will help you to achieve this.

Community
  • 1
  • 1
himanshu
  • 1,990
  • 3
  • 18
  • 36