When a user clicks on the home button, onPause() and onStop() gets called. When they click on the back button (to exit the app), onPause(), onStop(), and finally onDestory() gets called. Finally, when they press the power button, onPause() and onStop() gets called.
They all have onPause() being called first. Override onPause() and go back to the main activity using intent. You can also clear the stack of activities so they can't hit the back button to go back into one.
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button goPrivate = (Button) findViewById(R.id.goPrivate);
goPrivate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Private.class));
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.vzw.www.myapplication.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MAIN"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="GO TO PRIVATE"
android:id="@+id/goPrivate"/>
</RelativeLayout>
Create a new activity. I named mine Private.java
Private.java
public class Private extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_private);
}
@Override
protected void onPause() {
super.onPause();
startActivity(new Intent(Private.this, MainActivity.class));
}
}
activity_private.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.vzw.www.myapplication.Private"
android:layout_gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PRIVATE!"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Just create a blank project in Android Studio and try this. It works! :)
What happens if we click into ANOTHER activity and go deep into the app?
Create a boolean inside of Private class.
boolean goSuperClicked = false;
Updated private class is below:
public class Private extends AppCompatActivity {
Button goSuper;
boolean goSuperClicked = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_private);
goSuper = (Button) findViewById(R.id.goSuper);
goSuper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
goSuperClicked = true;
startActivity(new Intent(Private.this, SuperPrivate.class));
}
});
}
@Override
protected void onPause() {
super.onPause();
Log.d("onPause()", "onPause called");
if (!goSuperClicked) {
startActivity(new Intent(Private.this, MainActivity.class));
}
}
}