1

I am using Android Studio to make an application that starts with activity A and then with a button press goes to activity B that contains some other buttons that extend into their own activities.

I want to use the back button to just go back to the previous activity - which I read was the default action in the android devs documentation page... However, my app just exits when I am on activity B or one of the others that extend it when i press the back.

I think my problem is that I am not pushing my activities to the stack, so that's why it exits? Because there is nothing in the stack?

I have read on a lot of questions this same question, but I still understand.

So if I have two Java classes Activity A and Activity B and a Main.

The main will use an intent to start the activity A. And then through a button in A, activity B will open. Now i want to press back on my device and it goes back to A. And if i press back in A, it exits.

what i found which i should use?

 @Override
   public void onBackPressed() {
    // do something on back.
    return;
  }

I have called activity A and B differently.

A is Timer and B is aboutme_help.

Timer:

public void openAboutme(View view) {
    setContentView(R.layout.activity_aboutme_help);
}

aboutme_help:

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


 public class aboutme_help extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_aboutme_help);
}


  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_aboutme_help, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

       return super.onOptionsItemSelected(item);
   }
  }

Perhaps a sample application some one can point me to that contains this simple functionality.

6
  • So... you mean home button or back button? These are two separate things. Commented Apr 5, 2015 at 18:06
  • What does // do something on back actually do and also why are you overriding onBackPressed() in the first place? You haven't shown enough code - for instance you haven't shown how Activity A actually starts Activity B. Please edit your question and add more specific detail.
    – Squonk
    Commented Apr 5, 2015 at 18:17
  • Also: How do you start activity B, from activity A?
    – MTilsted
    Commented Apr 5, 2015 at 18:19
  • Did you by any chance call finish() after starting activity B from activity A? Commented Apr 5, 2015 at 18:23
  • Um... there's no "main" in android. When you debug the first defined activity with launcher intent-filter in manifest will be started automatically. So what does your main do? Commented Apr 5, 2015 at 18:25

2 Answers 2

1

You can simply set up the manifest file so that Activity A is the Parent of Activity B. Thus it will know to go back when pressed. So in the manifest file under the acitivity sect for Activity B :

 android:parentActivityName="com.package.ActivityA"

ActivityA is the name of your java class and not the XML file name and com.package is the name of your package

1
  • 1) This is for "up" button not "back" button. 2) Will work for "up" button out of the box only on Android 4.2 or higher. Commented Apr 5, 2015 at 18:32
0

public void openAboutme(View view) { setContentView(R.layout.activity_aboutme_help); }

What this does is it replaces the content of current activity with a new layout. If you call this in activity A, you never leave activity A, so back button naturally closes the app if it's the first activity.

This is how you start an activity in Android:

Intent i = new Intent(this, ActivityB.class);
startActivity(i);

Don't forget to define your activities in the manifest.

Note 1:

Please follow Java naming conventions. In Java class names are in pascal case (FirstLetterOfWordIsCapital). AboutActivity is certainly a better name as one can see it is an activity by looking at the class name.

Note 2:

Your tutorial should have mentioned how to start another activity in the first place. Update your question with a link to your tutorial.

12
  • thanks, here is the tutorial developer.android.com/training/basics/firstapp/… all my activities in manifest are red but the application works.
    – user4038552
    Commented Apr 5, 2015 at 18:51
  • Where do I add Intent i = new Intent(this, ActivityB.class); startActivity(i); I am using android:onClick="openAboutme" (log is a button that opens another activity)
    – user4038552
    Commented Apr 5, 2015 at 18:57
  • C'mon, you're not even trying! So when you click the button whatever is inside openAboutme gets executed. Now I told you that what you currently have inside openAboutme is not what you want. So now you tell me, where would you put the Intent snippet? Commented Apr 5, 2015 at 19:04
  • It is now working from going to about me to the help menu again thanks! :D I updated my code.
    – user4038552
    Commented Apr 5, 2015 at 19:05
  • Good job, no problem! Commented Apr 5, 2015 at 19:07