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.
// do something on back
actually do and also why are you overridingonBackPressed()
in the first place? You haven't shown enough code - for instance you haven't shown howActivity A
actually startsActivity B
. Please edit your question and add more specific detail.finish()
after starting activity B from activity A?