0

In my app, I have only one Activity which hosts several fragments.

The layout of my activity is(main.xml):

<LinearLayout...>

    <FrameLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"   
        android:id="@+id/fragment_placeholder">
     </FrameLayout>

</LinearLayout>

My only Activity:

public class MyActivity extends FragmentActivity{
   ...
   @Override
   protected void onCreate(Bundle arg0) {
       super.onCreate(arg0);
       setContentView(R.layout.main);

       //I dynamically add fragments into fragment_placeholder of the layout

       FirstFragment firstFragment = new FirstFragment();

       FragmentManager fragmentManager = getSupportFragmentManager();
       FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

       fragmentTransaction.add(R.id.fragment_placeholder, FirstFragment, "first");
       fragmentTransaction.commit();
   }


}

I my above activity, I dynamically add the first fragment to layout. The other fragments replace this fragment accordingly.

I know when user press back button to exit the app, by default, my app will still run on background.

what I want however is to kill the app process when user seeing the firstFragment and press the back button (exits the app). But how can I kill my app technically(programmatically) in Android?

Leem.fin
  • 40,781
  • 83
  • 202
  • 354

2 Answers2

3

I end up killing my app process by:

android.os.Process.killProcess(android.os.Process.myPid())
Leem.fin
  • 40,781
  • 83
  • 202
  • 354
2

you can override the back button behaviour by using below code and kill your activity

          public void onBackPresed(){

                  finish();

                     } 
Nikhil Lamba
  • 593
  • 1
  • 6
  • 17