0

I actually have an aplication with multiple activitys (on for each icon of my toolbar), also I would like to have the possibility to show the last fragment of these activitys from the stack if the user already open it before. As you can see in my BaseActivity class below (which I extend for all my activitys), I already add fragments to the stack.

This is my class :

public class BaseActivity extends AppCompatActivity {

private final String TAG = "BaseActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_base);
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    return super.onOptionsItemSelected(item);
}

public void setImageLoaderConfig(Context context) {
    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .cacheInMemory(true).cacheOnDisk(true)
            .resetViewBeforeLoading(true)
            .build();
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
            .defaultDisplayImageOptions(defaultOptions)
            .build();
    ImageLoader.getInstance().init(config);
}

public void initToolBar() {
    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.toolbar_layout);
    Toolbar toolbar = (Toolbar) linearLayout.findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    if (toolbar != null) {
        initToolbarOnClickListener();
    }
}

private void initToolbarOnClickListener() {

    (findViewById(R.id.toolbar_relativelayout_news)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showActivity(ArticlesActivity.class);
        }
    });
    (findViewById(R.id.toolbar_relativelayout_toilettes)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showActivity(ToilettesActivity.class);
        }
    });
    (findViewById(R.id.toolbar_relativelayout_practice)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showActivity(PratiquesActivity.class);
        }
    });
    (findViewById(R.id.toolbar_relativelayout_diary)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showActivity(AgendaActivity.class);
        }
    });
    (findViewById(R.id.toolbar_relativelayout_forum)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showActivity(ForumActivity.class);
        }
    });
}

public void updateToolbarIcons(Integer position) {
    ImageView imageViewIcon;
    Integer[] activeDrawableIds = {R.drawable.menu_actu_hover_320, R.drawable.menu_toilette_hover_320,
            R.drawable.menu_pratiques_hover_320, R.drawable.menu_agenda_hover_320, R.drawable.menu_forum_hover_320};
    Integer[] inactiveDrawableIds = {R.drawable.menu_actu_160, R.drawable.menu_toilette_320,
            R.drawable.menu_pratiques_320, R.drawable.menu_agenda_320, R.drawable.menu_forum_320};
    Integer[] imageviewIds = {R.id.toolbar_imageview_news, R.id.toolbar_imageview_bathroom,
            R.id.toolbar_imageview_practice, R.id.toolbar_imageview_diary, R.id.toolbar_imageview_forum};
    for (int i = 0; i < imageviewIds.length; i++) {
        Log.d(TAG, i + " " + imageviewIds[i]);
        imageViewIcon = (ImageView) findViewById(imageviewIds[i]);
        if (i == position) {
            imageViewIcon.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), activeDrawableIds[i]));
        } else {
            imageViewIcon.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), inactiveDrawableIds[i]));
        }

    }
    if (AfamiciApp.isConnected()) {
        ImageView imageViewUser = (ImageView) findViewById(R.id.toolbar_imageview_user);
        imageViewUser.setVisibility(View.VISIBLE);
    }
}

public void showActivity(Class activityClass) {
    Intent intent = new Intent(this, activityClass);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

public void initIsConnected() {
    if (AfamiciApp.getMail() != null && AfamiciApp.getPassword() != null) {
        AfamiciApp.setIsConnected(true);
    }
}

public void showConnexionFragment(Integer layoutId, Integer id) {
    Bundle args = new Bundle();
    args.putInt("id", id);
    args.putInt("containerId", layoutId);
    ConnexionFragment connexionFragment = new ConnexionFragment();
    connexionFragment.setArguments(args);
    getSupportFragmentManager()
            .beginTransaction()
            .replace(layoutId, connexionFragment, null)
            .addToBackStack(null)
            .commit();
}

public void showCreationCommentaireFragment(Integer layoutId, Integer id, Boolean isFromConnection) {
    if (isFromConnection) {
        getSupportFragmentManager().popBackStack();
    }
    Bundle args = new Bundle();
    args.putInt("id", id);
    CommentaireFragment commentaireFragment = new CommentaireFragment();
    commentaireFragment.setArguments(args);
    getSupportFragmentManager()
            .beginTransaction()
            .replace(layoutId, commentaireFragment, null)
            .addToBackStack(null)
            .commit();
}

public void showCreationCompteFragment(Integer layoutId, Integer id) {
    Bundle args = new Bundle();
    args.putInt("id", id);
    CreationCompteFragment creationCompteFragment = new CreationCompteFragment();
    creationCompteFragment.setArguments(args);
    getSupportFragmentManager()
            .beginTransaction()
            .replace(layoutId, creationCompteFragment, null)
            .addToBackStack(null)
            .commit();
}

}

Thanks in advance for your replys :)

Kupris
  • 165
  • 1
  • 12
  • what do you mean by last fragment? Last to be loaded (i.e., current fragment)? – Rohit Arya Apr 14 '16 at 18:28
  • For example, the user load ArticleActivity, which will load a first fragment in his framelayout and then another (with a replace) if he load something. After, he go on another activity and then click again on the bar icon of ArticleActivity. I would like to show the last fragment he see the first time he goes on. Don't know if it intelligible, my english is bad. – Kupris Apr 14 '16 at 18:31
  • Instead of launching activity on toolbar item selection, you can add/replace fragments using ViewPager. I don't think you need activities for your task. – Shadab Ansari Apr 14 '16 at 18:56

1 Answers1

1

You can get current fragment(last one added) in the FrameLayout using:

Fragment fragment = getSupportFragmentManager().findFragmentById(layoutId); //layoutId is id of FrameLayout
Rohit Arya
  • 6,751
  • 1
  • 26
  • 40
  • That sound good but how can I get instance of previous activity loaded ? – Kupris Apr 14 '16 at 18:34
  • @Kupris, Let me clarify, if you go back to `ArticleActivity` you want see the last fragment which you had loaded? – Rohit Arya Apr 14 '16 at 18:38
  • It's that, let's assume I click on toolbar icon which supposed to show me it (actually I only create a new instance with default fragment adding). – Kupris Apr 14 '16 at 18:40
  • Okay, I am little confused but if you want to open `ArticleActivity` in the same state you can use this intent flag. `intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);` for re launching `ArticleActivity` – Rohit Arya Apr 14 '16 at 18:45
  • And in manifest of your `ArticleActivity` add this: `android:launchMode="singleTask"` – Rohit Arya Apr 14 '16 at 18:52
  • That sound good for my articleactivity but I set singleTask for all of my activitys and it's the only which go back in his previous state. – Kupris Apr 14 '16 at 19:01
  • if you want same thing for all activities you can do that. But also remember to add intent flags. Either: `intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);` OR `intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);` depending on your use cases. – Rohit Arya Apr 14 '16 at 19:03
  • read [this](http://stackoverflow.com/questions/15359124/resume-the-activity-instead-of-starting-if-already-exists-in-back-stack) to understand more. – Rohit Arya Apr 14 '16 at 19:05
  • I add the FLAG_ACTIVITY_REORDER_TO_FRONT flag in my showActivity, so all of my Activity get the flag, and I add the singletask for all of my activitys in my manifest, maybe it's that which cause problem. – Kupris Apr 14 '16 at 19:05
  • then don't add flag for all activities, totally depends on your use case. – Rohit Arya Apr 14 '16 at 19:06
  • I just need the same behavior for each of my activitys, resume them instead of recreate them each time. So if I add this flag at each of them I'm good logically ? – Kupris Apr 14 '16 at 19:08
  • okay then add `intent` flags and also modify manifest. Hope this solves your problem? – Rohit Arya Apr 14 '16 at 19:09
  • If I change android:launchMode="singleTask" by android:launchMode="singleInstance" It seems to works, thanks a lot for your help :) – Kupris Apr 14 '16 at 19:12