Pergunta

Use of this code I call Next Activity.

Code

public void click_contact(View v)
{
 Intent myIntent = new Intent(MainActivity.this, ContactActivity.class);
 MainActivity.this.startActivity(myIntent);
 overridePendingTransition (R.anim.slide_in_right, R.anim.slide_out_left);
 finish();
}

So 'ContactActivity' is lunched. when the built-in "back" button from my device is pressed,the previous activity ('MainActivity') is closed. But I want relaunch this activity.

Foi útil?

Solução 5

If you want restore your MainActivity then you should remove finish() from your code. Code

public void click_contact(View v)
{
    Intent myIntent = new Intent(MainActivity.this, ContactActivity.class);
    MainActivity.this.startActivity(myIntent);
    overridePendingTransition (R.anim.slide_in_right, R.anim.slide_out_left);
}

If you want restart MainActivity on back in ContactActivity then you should start it in onBackPressed():

Code

@Override
public void onBackPressed() 
{
    Intent myIntent = new Intent(ContactActivity.this, MainActivity.class);
    startActivity(myIntent);
    super.onBackPressed();
}

Outras dicas

Remove the finsh(); at last line and try. Then you will get your Main Activity on pressing back button.

Hope it will help you.

Overriding backPress will start new intent. That is not good! just dont right finish(). I guess that should automatically take u to the previous activity.

this is your code

    public void click_contact(View v){
    Intent myIntent = new Intent(MainActivity.this, ContactActivity.class);
    MainActivity.this.startActivity(myIntent);
    overridePendingTransition (R.anim.slide_in_right, R.anim.slide_out_left);
    finish();
}

Why you use finish() .By using finish you are removing or the activity(in your case MainActivity) from the activity stack .Or you can say the activity will not get added to stack . So on back press there will not be any activity to load . So do not use finish()

Just use

startActivity(new Intent(ContactActivity.this, MainActivity.class));
overridePendingTransition (R.anim.slide_in_right, R.anim.slide_out_left);

So by removing finish your MainActivity will get added to the activity stack and on back press in your ContactActivity it will automatically load your MainActivity

Have an eye over this

Did you remove finish() in MainActivity?

Then, Check the FLAG on the intent which started the MainActivity. You might have used FLAG_NO_HISTORY. Which means the activity will be finish when it goes to background.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top