0

I'm currently working on a project that has an activity which is consisted of two fragments.

  • The first fragment shows a custom expandable list. Every row is created from a custom layout that has a checkbox in the right side of it.
  • The second fragment shows more details about the clicked row from the list. In order to open the second fragment, the user has to click on the row. The checkbox is used for another reason.

So, what I'm trying to do is to display these two fragments side by side only when the application runs in tablets. When the app runs in handsets and the user presses one row, the second fragment should be displayed on top.

Furthermore, I have an action bar at the top of the screen which has implemented the usual back button.

The problem exists when I open the second fragment when I have already selected some checkboxes. When I press the back button, which navigates me to the first fragment, the checkboxes will not be checked.

The onSaveInstanceSate method is obviously not called (as the parent activity is not getting paused), so I can't save the ArrayList that stores the checked rows.

Last but not least, the fragments are being added dynamically.

The question

How can I properly implement the back button so when the user uses a

  1. tablet, the back button should be used in order to close the activity, or a
  2. handset, so the back button should be used as a navigation back to the first fragment with the ability to restore it's previous state?
  • So the real problem is the checkbox not saving checked state correct? If this is true, make sure you call setRetainInstanceState(true) on your list fragment when you create it. For the layout issue, create two different layouts and let android choose the appropriate one. – Scott Naef Apr 10 '14 at 13:57
  • First of all, sorry @ScottNaef for my late comment and thank you for your replay. The setRetainInstanceState(true) doesn't seem to solve my problem. The two-layout trick is not an option for me. I think that you suggest two different layouts, where the first declares the first fragment in the layout directory, while the other declares two fragments in the layout-large directory. Well, I add my fragments dynamically. If that is what you really mean, it may not be the answer I'm looking for. – Michael Bakogiannis Apr 14 '14 at 19:54

3 Answers3

1
if (mFragmentManager.getBackStackEntryCount() == 0) {
        LogUtil.d(TAG,
                "home fragment" +      mFragmentManager.getBackStackEntryCount());

        this.finish();
    } else {

        mFragmentManager.popBackStackImmediate();


        }

try this should work , happy coding

Amit
  • 391
  • 3
  • 15
  • Thanks for aswering!!! Well, my backStack has always 0 size. I also call the addToBackStuck() method. I implemented this in my onBackPressed() method in the parent activity that hosts the two fragments. – Michael Bakogiannis Apr 14 '14 at 20:27
0

My first idea would be to create a boolean in the resources of your project: in the "values" directory, your boolean would be false, for example, and in your "values-sw600dp" and "values-sw720dp-land" directory, the boolean would be true.

Then, in your code, you would check the boolean (using R.boolean.your_boolean) to know if this is a tablet or a handset.

Then, with a simple if/else, you would implement your code, depending on the value of your boolean...

if(yourBoolean){
//We are on a tablet
finish();
}else{
//We are on a handset
//Your code to navigate back...
}
Jeje Doudou
  • 1,707
  • 1
  • 16
  • 24
0

You need to (1) detect if the user is on a tablet and (2) control the back function accordingly. I'm not sure how you're currently detecting whether the device is a tablet but a very easy method is described here. It involves a boolean resource that you can access when customizing your back function to determine the device type.

What I would do is override onBackPressed in your hosting Activity and control back function from there

@Override
public void onBackPressed(){
    boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
    if (tabletSize){
        moveTaskToBack(true);
    } else {
       //handle fragment back stack  
    }
}

The info on handling the back stack and replacing the fragment is here in the android docs. I will update that section later but I have to run for now.

Community
  • 1
  • 1
Rarw
  • 7,645
  • 3
  • 28
  • 46