Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8538575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:08:58+00:00 2026-06-11T11:08:58+00:00

I have an Activity that Displays Fragments one at a time , now each

  • 0

I have an Activity that Displays Fragments one at a time , now each fragment load it data from server and keep fetching data in a list , the user can display another fragment in the activity by selecting an item in the Spinner that displayed in the ActonBar.

here is the code for the Activity

public class HomeActivity  extends SherlockFragmentActivity .....{

        private class ListInfo {


                private String tag;
        private Class<?> clss;
        private Bundle bundle;
        private Fragment fragment;

    public ListInfo(String tag, Class<?> clss, Bundle bundle) {
        this.tag = tag;
        this.clss = clss;
        this.bundle = bundle;
    }

}

String[] naviStrings;
private HashMap<String, ListInfo> listMap = new HashMap<String, HomeActivity.ListInfo>();

private ListInfo mLastListInfo = null;

private int currentSelectedOptionInSpinner;
ChannelFragment fr;

@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);
    Log.d("HomeActivity", "onCreate");
    Log.d("onCreate bundle", "" + arg0);
    setContentView(R.layout.test);
    naviStrings = getResources().getStringArray(
            R.array.action_bar_spinner_entries);

    initializeList(arg0);

    Context context = getSupportActionBar().getThemedContext();
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            context, R.array.action_bar_spinner_entries,
            R.layout.sherlock_spinner_item);

    adapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    getSupportActionBar().setListNavigationCallbacks(adapter, this);
    getSupportActionBar().setSelectedNavigationItem(
            currentSelectedOptionInSpinner);

}

private void initializeList(Bundle args) {

    ListInfo listInfo = null;

    // add first Fragment

    listMap.put(naviStrings[5], (listInfo = new ListInfo(naviStrings[5],
            AboutUsActivity.class, args)));
    addFragmentToList(this, listInfo);

    listMap.put(naviStrings[3], (listInfo = new ListInfo(naviStrings[3],
            Fragment1.class, args)));
    addFragmentToList(this, listInfo);

    listMap.put(naviStrings[2], (listInfo = new ListInfo(naviStrings[2],
            Fragment2.class, args)));
    addFragmentToList(this, listInfo);

    listMap.put(naviStrings[1], (listInfo = new ListInfo(naviStrings[1],
            Fragment3.class, args)));
    addFragmentToList(this, listInfo);

    listMap.put(naviStrings[4], (listInfo = new ListInfo(naviStrings[4],
            Fragment4.class, args)));
    addFragmentToList(this, listInfo);

    if (args != null) {
        Toast.makeText(this, "args is not null", Toast.LENGTH_SHORT).show();
        // set the current selected index in the ActionBar spinner
        if (args.getInt(
                ApplicationMetaData.IntentData.LAST_SELECTED_ITEM_SPINNER,
                -1) > -1
                && args.getInt(
                        ApplicationMetaData.IntentData.LAST_SELECTED_ITEM_SPINNER,
                        -1) < naviStrings.length) {
            Toast.makeText(this, "selected  is not null",
                    Toast.LENGTH_SHORT).show();

            currentSelectedOptionInSpinner = args
                    .getInt(ApplicationMetaData.IntentData.LAST_SELECTED_ITEM_SPINNER);
        } else {
            currentSelectedOptionInSpinner = 2;
        }
    } else {
        currentSelectedOptionInSpinner = 2;
    }

    onNavigationItemSelected(currentSelectedOptionInSpinner, 0);
}

private static void addFragmentToList(SherlockFragmentActivity activity,
        ListInfo instanse) {
    // check to see if we already have a fragment for this tab , probably
    // from a previously save state.
    // if so deactivated it ,because our initial state is that a tab is not
    // shown.
    String tag = instanse.tag;

    instanse.fragment = activity.getSupportFragmentManager()
            .findFragmentByTag(tag);

    if (instanse.fragment != null && !instanse.fragment.isDetached()) {
        FragmentTransaction ft = activity.getSupportFragmentManager()
                .beginTransaction();
        ft.detach(instanse.fragment);
        ft.commit();
        activity.getSupportFragmentManager().executePendingTransactions();
    }

}



@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt(
            ApplicationMetaData.IntentData.LAST_SELECTED_ITEM_SPINNER,
            currentSelectedOptionInSpinner);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    Log.d("onNavigationItemSelected", "" + itemPosition);
    if (itemPosition == 5) {
        Intent i = new Intent(this, AboutUsActivity.class);
        startActivity(i);
        return true;
    }
    Log.d("fragment data", listMap.get(naviStrings[itemPosition]) + "");
    ListInfo newtItem = listMap.get(naviStrings[itemPosition]);
    if (newtItem != mLastListInfo) {
        FragmentTransaction ft = this.getSupportFragmentManager()
                .beginTransaction();

        if (mLastListInfo != null) {
            if (mLastListInfo.fragment != null) {
                ft.detach(mLastListInfo.fragment);
            }
        }
        if (newtItem != null) {
            if (newtItem.fragment == null) {
                // create and add
                newtItem.fragment = Fragment.instantiate(this,
                        newtItem.clss.getName(), newtItem.bundle);

                ft.add(android.R.id.content, newtItem.fragment,
                        newtItem.tag);
            } else {
                ft.attach(newtItem.fragment);
            }
        }
        mLastListInfo = newtItem;
        ft.commit();
        this.getSupportFragmentManager().executePendingTransactions();
        return true;
    }

    return false;

}}

now when i navigate from one fragment to another lets say from Fragment1 to Fragment2 when i return back to fragment1 it preserve it state and does not have to load it data from the beginning , but if i start a new Activity from the home Activity the system destroy the Activity and the Fragments in it , is There a way to preserve these Fragment note when i rotate the Home Activity nothing happened , only if i start a new Activity ??????

UPDATE
in all of the Four Fragment i make in the onCreate setRetaineInstance(true);

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:08:59+00:00Added an answer on June 11, 2026 at 11:08 am

    One option is to override saveInstanceState in your Fragments and/or Activites in order to persist data and later retrieve it from the Bundle that gets passed into onActivityCreated/onCreate.

    If you are dealing with large sets of data, you may be better off using another persistence mechanism like the SQLite database

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an activity that I'd like to load from a preference screen. I'm
I have a main activity that takes elements from a database and displays them
I have an activity that displays a number. The number is obtained from SharedPreferences
I have an Activity that displays different Fragments and these fragments changed when the
I have an app that displays a splash screen. The splash screen activity creates
I have an Activity that displays comments. The comments themselves have a layout, so
I have an Android Activity that displays Google Maps through MapView control and extending
I have an Android activity that displays a list of log entries (using a
So I have an Activity which has a FragmentPagerAdapter and inside each fragment I
I have an activity that calls the new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to); setListAdapter(notes);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.