Leem.fin
Leem.fin

Reputation: 42642

kill my app process when exit the app by press back button

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?

Upvotes: 0

Views: 8284

Answers (2)

Leem.fin
Leem.fin

Reputation: 42642

I end up killing my app process by:

android.os.Process.killProcess(android.os.Process.myPid())

Upvotes: 3

Nikhil Lamba
Nikhil Lamba

Reputation: 603

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

          public void onBackPresed(){

                  finish();

                     } 

Upvotes: 2

Related Questions