0

I have some troubles with amount of instance my app. In some cases my application has more that one instance in list of recent cards.

Case 1

Noramlly open app. Action main is SplashScreen.class > MainActivity.class > Fragment(view). In this case application has only one instance in recent cards.

Case 2

Application is open, then can open app through mail box, specially from gmail, email include attachment which can open my app. Flow to this case SharedData.class > MainActivity.class > Fragment(view). Now I have two instance of app.

enter image description here

This what I would have is close previous instance and start new or open attachment in first instance.

Tested some parameters for activity from documentation App manifest file - activity. Result is not satisfied, specially tested lunchmode ("singleTop","singleTask" and "singleInstance"). Close one is "singleTask" which close previous instance, but must click twice attachment to open new instance (Instance exist > click attachment > First instance is close (not exist any instance) > click attachment > open new instance).

If someone has advice how resolve this problem I will be appreciate. This problem exist in many combination on stack but none deal with it.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Developer534255
  • 121
  • 1
  • 9
  • Put your code, please. So it's easy for us to fix. – Fahry Mohammed Nov 27 '19 at 14:37
  • Yeap, it can be problematic because there are not a few lines, if you write which part of the code you are interested in, I can paste it here – Developer534255 Nov 27 '19 at 14:55
  • Specifically, how you transecting the fragments. – Fahry Mohammed Nov 27 '19 at 16:03
  • In most cases, I replace the fragment public void replaceFragment(Fragment newFragment, String tag) { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.container_fragment, newFragment, tag).addToBackStack(tag).commit(); } – Developer534255 Nov 28 '19 at 10:26
  • It is hard to conclude the problem cause without the code. However, seems because you haven't checked the back stack existing fragment. So you need to check whether all previous fragments are closed or not. – Fahry Mohammed Nov 29 '19 at 05:50

2 Answers2

0

This is happening because you just adding the fragment without checking whether is already available or not. So just check before you add a fragment.

 public void replaceFragment(Fragment newFragment, String tag)
 {
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction ft = manager.beginTransaction();
    if (manager.findFragmentByTag(tag) == null) {
        ft.replace(R.id.container_fragment, newFragment, tag)
                .addToBackStack(tag).commit();
    }else {
        ft.show(manager.findFragmentByTag(tag)).commit();
    }
 }

If curious learn more about the fragment please take a look here and go through the official documentation.

Fahry Mohammed
  • 599
  • 5
  • 18
0

I search little later and found solution:

set android:launchMode="singleTask" in AndroidManifest on

<activity
    android:launchMode="singleTask"
    android:name=".MainActivity">
</activity>

then in SharedData.class set

private void startMainActivityAndFinishCurrent() {
    Intent intent = new Intent(ContextInstance.getContext(), MainActivity.class);
    startActivity(intent);
    finish();
}

Thats it, but what's most interesting thing is understand activity tag in manifest

Developer534255
  • 121
  • 1
  • 9