Tuesday, May 22, 2012

Re: [android-developers] onSaveInstanceState method on Fragment not called when corresponding FragmentActivity acts as a tab.

(1) If you are using fragments, don't use ActivityGroup or LocalActivityActivityManager.  "This class is deprecated. Use the new Fragment and FragmentManager APIs instead; these are also available on older platforms through the Android compatibility package."  http://developer.android.com/reference/android/app/ActivityGroup.html 

(2) For the love of all, ghod please do not instantiate a LocalActivityManager directly: "This class is not normally used directly, but rather created for you as part of the ActivityGroup implementation."   http://developer.android.com/reference/android/app/LocalActivityManager.html 

(3) Use the various sample code that shows how to use tabs with fragments, such as http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/FragmentTabs.html or http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabs.html or http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabsPager.html

On Tue, May 22, 2012 at 10:25 AM, Bluemercury <joao.rossa@gmail.com> wrote:
Hi! so im currently having a problem regarding the onSaveInstaceState method in the Fragment class. This class is called from a FragmentActivity that acts as a Tab created in another FragmentActivity, but i cant seem to make the call to onSaveInstanceState to happen, here's some code:

mainmenu class that generates the tabs:

public class MainMenu extends FragmentActivity{

private TabHost tabs=null;

public String getWindowTitle() {
return "Home";
}

//@Override
public int getLayoutId() {
return R.layout.main_menu;
}

//@Override
public int getThemeId() {
return R.style.CustomTheme;
}

@Override
public void onSaveInstanceState(Bundle bundle) {

super.onSaveInstanceState(bundle);
bundle.putInt("TAB",tabs.getCurrentTab());
}

@Override
public void onRestoreInstanceState(Bundle bundle) {

super.onRestoreInstanceState(bundle);
tabs.setCurrentTab(bundle.getInt("TAB"));


}

@Override
public void onCreate(Bundle savedInstanceState) {

setTheme(getThemeId());
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
LocalActivityManager mlam = new LocalActivityManager(this, false);
tabs=(TabHost)findViewById(android.R.id.tabhost);
mlam.dispatchCreate(savedInstanceState);
tabs.setup(mlam);

TabHost.TabSpec spec;
Intent intent;

// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, MyProcessListFragmentActivity.class);
spec = tabs.newTabSpec("Home").setIndicator((buildTabIndicator("Home")))
.setContent(intent);
tabs.addTab(spec);

// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, SearchProcess.class);
spec = tabs.newTabSpec("Search").setIndicator((buildTabIndicator("Search")))
.setContent(intent);

// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, MyProcessList.class);
tabs.addTab(spec);
spec = tabs.newTabSpec("Options").setIndicator((buildTabIndicator("Options")))
.setContent(intent);

tabs.addTab(spec);

tabs.setCurrentTab(0);


}

Here's the MyprocessListGragmentActivity:

public class MyProcessListFragmentActivity extends FragmentActivity implements MyProcessFragmentList.onProcessSelectedListener{

@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);

setContentView(R.layout.myprocesslist_fragment);
}
}

and the Fragment class MyProcessFragmentList called  from the layout :

public class MyProcessFragmentList extends IpdmsCoreFragment {

onProcessSelectedListener mListener;

private PullToRefreshListView mPullRefreshListView;

private LinkedList<GenericMenuItemListDTO> iTems;
private List<MyProcessDTO> tempList;
private GenericMenuAdapter adapter;

private int processListPage=1;


// Container Activity must implement this interface
public interface onProcessSelectedListener{

public void onArticleSelected(MyProcessDTO process);
}


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

try{
mListener=(onProcessSelectedListener) activity;
}catch(ClassCastException e){
throw new ClassCastException(activity.toString() + " must implement onProcessSelectedListener");
}
}



@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

outState.putSerializable("LISTPROCESS", adapter.getAllItems());
}

The onSaveInstanceState never gets called on rotation....but if i run the Fragment Activity directly it works and it gets called....

Regards, 
 
 

 

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en



--
Dianne Hackborn
Android framework engineer
hackbod@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate