Wednesday, 18 February 2015

How to Finish Parent Activity from Child Activity in Android

In previous post i explained "how to back from child activity to fragment"


Here i will explain how to finish parent activity from child activity in Android..

Parent Activity


public class HomeScreen extends Activity
{
      static HomeScreen firstActivity ; //Create one Activity variable"firstActivity "
      @Override
       protected void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
setContentView(R.layout.newhome);
        firstActivity= this;
}


        public static HomeScreen getActivity() 
                          //Declare one function "getActivity()"
          {
            return   firstActivity ;  
                     // Assign Activity "HomeScreen"  to Activity Variable "firstActivity"
          }


}



In Child activity call the getActivity() function to finish the child activity.HomeScreen.getActivity().finish();  
                 // the fuction returns the Activity variable "firstActivity"

Happy Coding :)