0

I have a NavigationViewer activity which has 3 fragments. I want that every time the user selects an item from NavigationViewersliding menu items the app will transact a new fragment object of the selected kind.

for Example I have a NavigationViewer menu item Called "MyFragment" So I am trying this Code on that item:

MyFragment myFragment = new MyFragment();
fragmentTransaction.replace(R.id.RR, myFragment , "nav_MyFragment ").commit();

But this causes a problem that if user selected "MyFragment" from menu while it is active [seen to user] it will create a new object. and I want to create that new object only if transacting from some fragment to another.

Any Suggestions?

Edit: retrieving the fragment by tag and then checking if isVisble() or if isAdded() gives null exception

Atef Hares
  • 4,715
  • 3
  • 29
  • 61

3 Answers3

1

You can keep a Fragment variable and assign your active fragment there, and then check the getClassName() and if it's the clicked one is the same as the one you are currently displaying, you don't load a new one.

I'm going to use the code from this app I have in github.

Then in your Activity declare a Fragment variable like this:

Fragment active;

In your onOptionsItemSelected() method, you have to do the checking for the item pressed.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    //noinspection SimplifiableIfStatement
    if (id == R.id.nav_item1) {
        if (active.getClass().getSimpleName().equals("FragmentA")) {
            // then this fragment is already being shown, therefore do nothing
        } else {
            FragmentA fragtoadd = new FragmentA();
            getFragmentManager().beginTransaction()
                                .remove(fragtoadd)
                                .commit();
            active = fragtoadd; // the magic is here, everytime you add a new fragment, keep the reference to it to know what fragment is being shown
        }
        return true;
    } else if (id == R.id.nav_item2) {
        // do the same as above but for FragmentB, and so on
        return true;
    } 
    // ... check all your items
    return super.onOptionsItemSelected(item);
}

If you have your code in Github so that I can have a look it would be better. Hope it helps!

Isaac Urbina
  • 1,295
  • 11
  • 21
0

U have to add fragment instance into backstack

getSupportedFragmentManager.addToBackStack(fragment.gettag)

Naitik
  • 101
  • 6
0

I assume you can detect the actual item clicks, so what I do is this:

private static final String TAG_FRAGMENT_SOME_NAME = "something";

mFragmentManager = getSupportFragmentManager(); // or getFragmentManager() if not using support library
mFragmentTransaction = mFragmentManager.beginTransaction();
Fragment myFragment = mFragmentManager.findFragmentByTag(TAG_FRAGMENT_SOME_NAME);
if (myFragment == null) {
    myFragment = MyFragment.newInstance();
}
mFragmentTransaction.replace(R.id.RR, myFragment, TAG_FRAGMENT_SOME_NAME).commit();
CounterFlame
  • 1,612
  • 21
  • 31
  • Can't resolve method `newInstance()` Note MyFragment extends android.app.Fragment – Atef Hares Apr 26 '16 at 18:46
  • newInstance() is a method you write yourself, as to not use the default empty constructor. [Check this answer](http://stackoverflow.com/a/9245510/3764592) for some advantages of how to do it/advantages to it. – CounterFlame Apr 26 '16 at 18:49
  • Yes, I just wrote it but this did not solve my problem because, as I wanted it prevented the recreation of `MyFragment` if [seen to user] but it also prevented the recreation of it when transacting from another fragment to `MyFragment` and I need to recreate it when this happens – Atef Hares Apr 26 '16 at 18:53