-3

I have 2 screens corresponding to 2 activities on my Android app. I am trying to go back to the first activity from the second screen by clicking a Back button. Even though my first activity restarts, I get an annoying pop-up message "Unfortunately the app has stopped" as my app refreshes to go back to the first activity. I would like to not have this message appear.

I have tried different variations of code and the message appears each time :

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

or

public void finishActivity(DisplayMessageActivity v){
  Intent intent = new Intent( this, MainActivity.class );
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);   
}

or even just

public void finishActivity(DisplayMessageActivity v){
    finish();
 }
pyqueen9
  • 7
  • 2
  • The [logcat will tell you what reason your app has stopped](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this). Please [edit] your question with it. – OneCricketeer Jan 23 '17 at 20:49
  • Thanks I did not think to check it - solved my issue! – pyqueen9 Jan 23 '17 at 21:25

1 Answers1

0

You should read the logs to find out what the exception is and why it is occurring. Places to check:

  1. If back navigation logic is inside an OnClickListener, something inside the OnCLickListener code is probably null.
  2. The activity it should go to after pressing back is having a null value for something you're trying to use.
  3. Any asynchronous calls from old activity finishing after it has been removed.

Your log should tell you exactly what it is.