Thursday, May 2, 2013

Different case in lifecycle of Activity and Fragment

It's part of the articles of lifecycle: start reading from Understand lifecycle of Activity and Fragment, Introduction.


I try to show the different case in lifecycle of Activity and Fragment in this post. Before I show the code, I show what found in my trial experiment.

case 1:
    Close app by HOME, become invisible:
  • MainActivity.onPause
  • MyFragment1.onPause
  • MainActivity.onStop
  • MyFragment1.onStop

  • back from closed app:
  • MainActivity.onRestart
  • MainActivity.onStart
  • MyFragment1.onStart
  • MainActivity.onResume
  • MyFragment1.onResume



case 2:
    Finished by calling finish() method (press the finish button):
  • MainActivity.onPause
  • MyFragment1.onPause
  • MainActivity.onStop
  • MyFragment1.onStop
  • MainActivity.onDestroy
  • MyFragment1.onDestroyView
  • MyFragment1.onDestroy
  • MyFragment1.onDetach

  • back from finished:
  • MainActivity.onCreate
  • MainActivity.onCreate / savedInstanceState == null
  • MainActivity.onStart
  • MyFragment1.onAttach
  • MyFragment1.onCreate
  • MyFragment1.onCreateView
  • MyFragment1.onActivityCreated
  • MyFragment1.onStart
  • MainActivity.onResume
  • MyFragment1.onResume

case 3:
    Orientation changed:
  • MainActivity.onPause
  • MyFragment1.onPause
  • MainActivity.onStop
  • MyFragment1.onStop
  • MainActivity.onDestroy
  • MyFragment1.onDestroyView
  • MyFragment1.onDestroy
  • MyFragment1.onDetach
  • MainActivity.onCreate
  • MyFragment1.onAttach
  • MyFragment1.onCreate
  • MainActivity.onCreate / savedInstanceState != null
  • MainActivity.onStart
  • MyFragment1.onCreateView
  • MyFragment1.onActivityCreated
  • MyFragment1.onStart
  • MainActivity.onResume
  • MyFragment1.onResume

case 3:
    Kill by system
    (refer this post to simulate activity killed by system)
  • MainActivity.onPause
  • MyFragment1.onPause
  • MainActivity.onStop
  • MyFragment1.onStop
  • MainActivity.onDestroy
  • MyFragment1.onDestroyView
  • MyFragment1.onDestroy
  • MyFragment1.onDetach

  • back from killed
  • MainActivity.onCreate
  • MyFragment1.onAttach
  • MyFragment1.onCreate
  • MainActivity.onCreate / savedInstanceState != null
  • MainActivity.onStart
  • MyFragment1.onCreateView
  • MyFragment1.onActivityCreated
  • MyFragment1.onStart
  • MainActivity.onResume
  • MyFragment1.onResume




The test code is modified version from the post "Understand lifecycle of Activity and Fragment". To make the main code clear, custom Activity (MyFragmentActivity extends FragmentActivity) and Fragment (MyFragment extends Fragment) were implemented to display status of lifecycle on Toast and log in LogCat.

MyFragmentActivity.java
package com.example.androidfragmenttest;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.Toast;

public class MyFragmentActivity extends FragmentActivity {
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  recLifeCycle();
  super.onCreate(savedInstanceState);
 }
 
 @Override
 protected void onStart() {
  recLifeCycle();
  super.onStart();
 }

 @Override
 protected void onRestart() {
  recLifeCycle();
  super.onRestart();
 }

 @Override
 protected void onResume() {
  recLifeCycle();
  super.onResume();
 }

 @Override
 protected void onPause() {
  recLifeCycle();
  super.onPause();
 }
 
 @Override
 protected void onStop() {
  recLifeCycle();
  super.onStop();
 }

 @Override
 protected void onDestroy() {
  recLifeCycle();
  super.onDestroy();
 }

 public void recLifeCycle(){
  
  String className = getClass().getSimpleName();
  StackTraceElement[] s = Thread.currentThread().getStackTrace();
  String methodName = s[3].getMethodName();
  
  Toast.makeText(getApplicationContext(), 
    className + "." + methodName, Toast.LENGTH_SHORT).show();
  Log.i("MYTAG", className + "." + methodName);
  
 }
 
 public void recLifeCycle(String note){
  String className = getClass().getSimpleName();
  StackTraceElement[] s = Thread.currentThread().getStackTrace();
  String methodName = s[3].getMethodName();

  Toast.makeText(getApplicationContext(), 
    className + "." + methodName, Toast.LENGTH_SHORT).show();
  Log.i("MYTAG", className + "." + methodName + " / " + note);
 }

}


MyFragment.java
package com.example.androidfragmenttest;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class MyFragment extends Fragment {

 @Override
 public void onAttach(Activity activity) {
  recLifeCycle();
  super.onAttach(activity);
 }

 @Override
 public void onCreate(Bundle savedInstanceState) {
  recLifeCycle();
  super.onCreate(savedInstanceState);
 }

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
  recLifeCycle();
  return super.onCreateView(inflater, container, savedInstanceState);
 }
 
 @Override
 public void onActivityCreated(Bundle savedInstanceState) {
  recLifeCycle();
  super.onActivityCreated(savedInstanceState);
 }

 @Override
 public void onStart() {
  recLifeCycle();
  super.onStart();
 }

 @Override
 public void onResume() {
  recLifeCycle();
  super.onResume();
 }

 @Override
 public void onPause() {
  recLifeCycle();
  super.onPause();
 }

 @Override
 public void onStop() {
  recLifeCycle();
  super.onStop();
 }

 @Override
 public void onDestroyView() {
  recLifeCycle();
  super.onDestroyView();
 }

 @Override
 public void onDestroy() {
  recLifeCycle();
  super.onDestroy();
 }

 @Override
 public void onDetach() {
  recLifeCycle();
  super.onDetach();
 }
 
 public void recLifeCycle(){
  
  String className = getClass().getSimpleName();
  StackTraceElement[] s = Thread.currentThread().getStackTrace();
  String methodName = s[3].getMethodName();
  
  Toast.makeText(getActivity(), 
    className + "." + methodName, Toast.LENGTH_SHORT).show();
  Log.i("MYTAG", className + "." + methodName);
 }
 
 public void recLifeCycle(String note){
  
  String className = getClass().getSimpleName();
  StackTraceElement[] s = Thread.currentThread().getStackTrace();
  String methodName = s[3].getMethodName();

  Toast.makeText(getActivity(), 
    className + "." + methodName, Toast.LENGTH_SHORT).show();
  Log.i("MYTAG", className + "." + methodName + " / " + note);
 }
 
}


MainActivity.java
package com.example.androidfragmenttest;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;

public class MainActivity extends MyFragmentActivity {

 static public class MyFragment1 extends MyFragment {

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
   super.onCreateView(inflater, container, savedInstanceState);
   View view = inflater.inflate(R.layout.fragment_layout1, null);
   return view;
  }

 }

 FrameLayout fragmentContainer;
 Button buttonFinish;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  buttonFinish = (Button)findViewById(R.id.finish);
  buttonFinish.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    finish();
   }});

  fragmentContainer = (FrameLayout) findViewById(R.id.container);
  if (savedInstanceState == null) {
   // if's the first time created
   recLifeCycle("savedInstanceState == null");
   
   MyFragment1 myListFragment1 = new MyFragment1();
   FragmentManager supportFragmentManager = getSupportFragmentManager();
   FragmentTransaction fragmentTransaction = supportFragmentManager
     .beginTransaction();
   fragmentTransaction.add(R.id.container, myListFragment1);
   fragmentTransaction.commit();

   
  }else{
   recLifeCycle("savedInstanceState != null");
  }
 }

}


/res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/finish"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="finish()"/>
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

</LinearLayout>


/res/layout/fragment_layout1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Fragment 1"/>
    
</LinearLayout>


Test different case in lifecycle of Activity and Fragment


download filesDownload the files.

* the original chart of lifecycle is taken here.

No comments: