1

I just started android programming and I created 3 activities.A button in each activity will open another.I can navigate from my 2nd activity back to my first but I can't navigate from my third activity back to my second.Here's what I tried but the app crashes when I click on the back button. I guess it's trying to jump from the third activity to the second. If it helps my second activity's java file is called CountryDetails.java

 public class WikiPage extends AppCompatActivity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);

        getSupportActionBar().setTitle(R.string.title_activity_wikipedia_details);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
Sook Lim
  • 541
  • 6
  • 28

3 Answers3

4

Simply call onBackPressed() instead of NavUtils.navigateUpFromSameTask(this)

Kruti Parekh
  • 1,271
  • 9
  • 21
3
  • Remove NavUtils.navigateUpFromSameTask(this);

You should use onBackPressed() only.

Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.

case android.R.id.home:
    onBackPressed();
    return true;
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

Use onBackPressed() instead of NavUtils.navigateUpFromSameTask(this) for activity backstake manage

user7176550
  • 1,501
  • 14
  • 19