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

  • Home
  • SEARCH
  • 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 9147077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:54:01+00:00 2026-06-17T10:54:01+00:00

in my app I’m using one activity and two fragments. The app uses a

  • 0

in my app I’m using one activity and two fragments. The app uses a layout with a container so the fragments are added via transactions. The first fragment contains a listview and the other fragment a detail view for the listview items.
Both fragments use setRetainInstance(true). The fragments are added via a replace transaction and addToBackStack(null) is set. The listfragment contains an instance variable which holds some infos for the list. Now I’m changing to detail and press back and the instance variable is null. I read about setRetainInstance and addToBackStack and removed addToBackStack, but even then the instance variable is null.

Does anyone know what I might be doing wrong?

regards,
Thomas

  • 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-17T10:54:03+00:00Added an answer on June 17, 2026 at 10:54 am

    setRetainInstance(true) will tell the FragmentManager to keep the fragment around when the containing Activity is killed and rebuilt for some reason. It doesn’t guarantee that the Fragment instance will stick around after a transaction to add or replace. It sounds like your adapter is being garbage collected and you’re not creating a new one.

    A more generally easy solution would be to make a viewless Fragment to retain your ListAdapter. The way you do this is to create the Fragment, set the retain instance to true, and return null in the method onCreateView(). To add it, just called addFragment(Fragment, String) via the FragmentTransaction. You never remove or replace it, so it will always stay in memory for the length of the app. Screen rotations won’t kill it.

    Whenever your ListFragment is created, in onCreateView() get the FragmentManager and use either the method findFragmentById() or FindFragmentByTag() to retrieve your retained fragment from memory. Then get the adapter from that fragment and set it as your adapter for the list.

    public class ViewlessFragment extends Fragment {
    
       public final static string TAG = "ViewlessFragment";
    
       private ListAdapter mAdapter;
    
       @Override
       public ViewlessFragment() {
          mAdapter = createAdater();
          setRetainInstance(true);
       }
    
       @Override
       public void onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          return null;
       }
    
       public ListAdapter getAdapter() {
          return mAdapter;
       }
    }
    
    public class MyListFragment extends ListFragment {
    
       final public static String TAG = "MyListFragment";
    
       @Override
       public void onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          final View returnView = getMyView();
          final ViewlessFragment adapterFragment = (ViewlessFragment) getFragmentManager().findFragmentByTag(ViewlessFragment.TAG);
          setListAdapter(ViewlessFragment.getAdapter());
          return returnView;
       }
    }
    
    public class MainActivity extends FragmentActivity {
    
       @Override
       public void onCreate(Bundle icicle) {
          // ... setup code...
          final FragmentManager fm = getSupportFragmentManager();
          final FragmentTransaction ft = fm.beginTransaction();
          ViewlessFragment adapterFragment = fm.findFragmentByTag(ViewlessFragment.TAG);
          if(adapterFragment == null) {
             ft.add(new ViewlessFragment(), ViewlessFragment.TAG);
          }
          ft.add(R.id.fragmentContainer, new MyListFragment(), MyListFragment.TAG);
          ft.commit();
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

App has fragment activity with framelayout container matching parent. User interacts with fragments ui
App Engine recently announced support for Cross Group (XG) transactions ; one would expect
We're building an app, our first using Rails 3, and we're having to build
App I'm working on uses ARC. I wanted it to process large files, so
My app should stream video's to an AppleTV via Airplay, even when the app
App.config looks like this: <appender name=MemoryAppender type=log4net.Appender.MemoryAppender> <layout type=log4net.Layout.PatternLayout> <conversionPattern value=%date [%thread] %-5level -
App is in prism/mvvm/mef and uses loading by attribute like StockTraderRI. My shell window
app.js var win1 = Titanium.UI.createWindow({ title:'Tab 1', backgroundColor: 'black', layout: 'vertical', }); win1.open(); Titanium.App.addEventListener('click',
App store approval guideline says that Apps that send Push Notifications without first obtaining
My app written in c# (winforms) launches a third party using Process.start(). After launch,

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.