Thursday, May 9, 2013

Lifecycle of dual fragment activity

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

In this exercise, we have two fragements; the first one (MyFragment0) have a button to switch to the second fragment (MyFragment1) using FragmentTransaction, the second fragment is same as the exercise of "UI elements lost after Activity/Fragment re-created" have images displayed. The lifecycle behavior is similar to "UI elements lost after Activity/Fragment re-created" or "How setRetainInstance(true) affect lifecycle of Fragment" depends on setRetainInstance(); except that if the activity's onCreate() method called with (savedInstanceState == null), it will reload the first fragment (MyFragment0), otherwise the second fragment (MyFragment1) will be kept displayed.

dual fragment activity


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.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends MyFragmentActivity {

static public class MyFragment0 extends MyFragment {

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

Button buttonSwFragment1 = (Button)view.findViewById(R.id.swfragment);
buttonSwFragment1.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
MyFragment1 myFragment1 = new MyFragment1();
FragmentManager supportFragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.container, myFragment1);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}});

return view;
}

}

static public class MyFragment1 extends MyFragment {

ImageView image_b, image_c, image_d;
TextView textState;
Button buttonLoadImage_d;

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

image_b = (ImageView)view.findViewById(R.id.image_b);
image_c = (ImageView)view.findViewById(R.id.image_c);
image_d = (ImageView)view.findViewById(R.id.image_d);
textState = (TextView)view.findViewById(R.id.textstate);
buttonLoadImage_d = (Button)view.findViewById(R.id.loadimage_d);

image_b.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));

if(savedInstanceState == null){
image_c.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
textState.setText("MyFragment1.savedInstanceState == null");
}else{
textState.setText("MyFragment1.savedInstanceState != null");
}

buttonLoadImage_d.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
image_d.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
}});

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

MyFragment0 myFragment0 = new MyFragment0();
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager
.beginTransaction();
fragmentTransaction.add(R.id.container, myFragment0);
fragmentTransaction.commit();
}
}

}


/res/layout/fragment_layout0.xml, layout of MyFragment0.
<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 0"/>

<Button
android:id="@+id/swfragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Switch to Fragment 1"/>

</LinearLayout>


For MyFragmentActivity.java, MyFragment.java, /res/layout/fragment_layout1.xml and /res/layout/activity_main.xml, refer to the exercise "UI elements lost after Activity/Fragment re-created".

download filesDownload the files.

No comments:

Post a Comment