1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.test.urgencecall.fragment.FavoriteListFragment;
import com.test.urgencecall.fragment.ProductListFragment;
public class HomeFragment extends FragmentActivity {
private Fragment contentFragment;
ProductListFragment pdtListFragment;
FavoriteListFragment favListFragment;
public HomeFragment() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
//------------------
//
//Importation du code favori
//
//------------------
FragmentManager fragmentManager = getSupportFragmentManager();
if (savedInstanceState != null) {
if (savedInstanceState.containsKey("content")) {
String content = savedInstanceState.getString("content");
if (content.equals(FavoriteListFragment.ARG_ITEM_ID)) {
if (fragmentManager.findFragmentByTag(FavoriteListFragment.ARG_ITEM_ID) != null) {
setFragmentTitle(R.string.favorites);
contentFragment = fragmentManager
.findFragmentByTag(FavoriteListFragment.ARG_ITEM_ID);
}
}
}
if (fragmentManager.findFragmentByTag(ProductListFragment.ARG_ITEM_ID) != null) {
pdtListFragment = (ProductListFragment) fragmentManager
.findFragmentByTag(ProductListFragment.ARG_ITEM_ID);
contentFragment = pdtListFragment;
}
} else {
pdtListFragment = new ProductListFragment();
setFragmentTitle(R.string.app_name);
switchContent(pdtListFragment, ProductListFragment.ARG_ITEM_ID);
}
return rootView;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
if (contentFragment instanceof FavoriteListFragment) {
outState.putString("content", FavoriteListFragment.ARG_ITEM_ID);
} else {
outState.putString("content", ProductListFragment.ARG_ITEM_ID);
}
super.onSaveInstanceState(outState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_favorites:
setFragmentTitle(R.string.favorites);
favListFragment = new FavoriteListFragment();
switchContent(favListFragment, FavoriteListFragment.ARG_ITEM_ID);
return true;
}
return super.onOptionsItemSelected(item);
}
public void switchContent(Fragment fragment, String tag) {
FragmentManager fragmentManager = getSupportFragmentManager();
while (fragmentManager.popBackStackImmediate()) ;
if (fragment != null) {
FragmentTransaction transaction = fragmentManager
.beginTransaction();
transaction.replace(R.id.frame_container, fragment, tag);
//Only FavoriteListFragment is added to the back stack.
if (!(fragment instanceof ProductListFragment)) {
transaction.addToBackStack(tag);
}
transaction.commit();
contentFragment = fragment;
}
}
protected void setFragmentTitle(int resourseId) {
setTitle(resourseId);
getActionBar().setTitle(resourseId);
}
/*
* We call super.onBackPressed(); when the stack entry count is > 0. if it
* is instanceof ProductListFragment or if the stack entry count is == 0, then
* we finish the activity.
* In other words, from ProductListFragment on back press it quits the app.
*/
@Override
public void onBackPressed() {
FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
super.onBackPressed();
} else if (contentFragment instanceof ProductListFragment
|| fm.getBackStackEntryCount() == 0) {
finish();
}
}
//---------------------
//
//Fin de l'importation
//
//---------------------
} |
Partager