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,684
  • 28
  • 81
  • 122

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
  • 51,611
  • 53
  • 174
  • 242
3

maybe just call finish() in your activity.

Natali
  • 2,922
  • 4
  • 37
  • 52
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,115
  • 7
  • 35
  • 57
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
  • 10
  • 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
  • 30,081
  • 6
  • 79
  • 95
Ramindu Weeraman
  • 334
  • 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,962
  • 3
  • 18
  • 36