0

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.

Zala Janaksinh
  • 2,929
  • 5
  • 32
  • 58
RaulPop
  • 25
  • 5
  • you are calling `finish` so your activity is destroyed. you will have to start the `MainActivity` again. But why do you want to do this? – Raghunandan Jun 25 '13 at 06:20
  • Remove finish() from your code. As per Activity life cycle for destroying an activity you have to call finish() method from activity. – Roll no1 Jun 25 '13 at 06:23
  • i want the users to be able to go back to the previous activity by pressing back button on their phone, but removing 'finish()' doesn't resolve the problem. with or without calling the 'finish()', pressing back closes my app. – RaulPop Jun 25 '13 at 06:25
  • @raulpop8 check this http://developer.android.com/design/patterns/navigation.html. Rethink your design. – Raghunandan Jun 25 '13 at 06:26
  • As others have mentioned, you shouldn't call `finish` and just allow the android system to control the back button. See (http://developer.android.com/guide/components/tasks-and-back-stack.html) for details on how it manages. If your app is closing when back is pressed, perhaps something else is happening that you're not aware of. Check logcat to see if there are any surprising messages. – brianestey Jun 25 '13 at 06:29
  • Means you need to Exit from app when user click on built-in "Back" button. – Mr.Sandy Jun 25 '13 at 06:36
  • @Raghunandan thanks! I will read that :) – RaulPop Jun 25 '13 at 06:36

5 Answers5

1

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

Hope it will help you.

Rushabh Patel
  • 3,052
  • 4
  • 26
  • 58
  • removing it doesn't resolve the problem. with or without calling the 'finish()', pressing back closes my app. – RaulPop Jun 25 '13 at 06:24
0

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.

Prachi
  • 3,584
  • 2
  • 24
  • 39
0

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

edwin
  • 7,985
  • 10
  • 51
  • 82
0

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.

Naresh
  • 3,174
  • 1
  • 17
  • 22
-1

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();
}
Grzegorz Żur
  • 47,257
  • 14
  • 109
  • 105
Dimmerg
  • 2,113
  • 1
  • 13
  • 14
  • You shouldn't have to override onBackPressed. This will add the MainActivity to the Activity back stack instead of going back to the MainActivity. (See http://developer.android.com/guide/components/tasks-and-back-stack.html) – brianestey Jun 25 '13 at 06:28
  • Ok. But what if I am using onBackPressed() in my MainAcitivity to show a AlertDialog, if the users want to close the app or not. Should I remove this dialog, or there is any other way to use both codes for onBackPressed()? – RaulPop Jun 25 '13 at 06:29
  • @raulpop8 I think overriding onBackPressed to show confirmation dialog is ok, but to navigate to the previous Activity it isn't required, and I believe, incorrect to do so. – brianestey Jun 25 '13 at 06:32
  • @brainestey To avoid the stacking the same activity you must use the FLAG "FLAG_BRING_TO_FRONT" in the intent. – Naresh Jun 25 '13 at 06:33
  • I suppose you could do that but I don't think that is necessary for the scenario the OP is trying to achieve. Showing a second Activity and then going back to the first is a common scenario and handled by Android. – brianestey Jun 25 '13 at 06:36
  • It depends of needed scenario. In some cases you need to restart activity instead of return to previous instance of backstack. – Dimmerg Jun 25 '13 at 06:39
  • used @Dimmerg way, and it worked. Now I am reading the links you guys left in comments. Thanks a lot for your answers! – RaulPop Jun 25 '13 at 06:55
  • 1
    Seriously Don't mind . You can obtain what you needed by this method .i don't think this is a right approach(Some exceptions may occur) – edwin Jun 25 '13 at 08:32