Chisko
Chisko

Reputation: 3107

My Activities and Fragments seem to be running methods twice after modifying their launchMode

I recently modified my project so I could handle a "Login task" which consists of a single activity, and a "Main task" which comprises all other activities. I did this via the Manifest:

<application
    android:name=".MyApp"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name">

    <activity
        android:name=".ui.login.LoginActivity"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:taskAffinity="@string/affinity_login">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".ui.main.MainActivity"
        android:label="@string/title_activity_main"
        android:taskAffinity="@string/affinity_main"
        android:launchMode="singleTask"/>

    <activity
        android:name=".ui.tandc.TermsAndConditionsActivity"
        android:parentActivityName=".ui.main.MainActivity" />

    ...

</application>

Now I'm starting to see duplicated lines every time I log something. I first thought that there were two instances of each activity/fragment (not sure how, really) but it's not the case as can be seen by logging this.toString() on activites and fragments:

09-25 16:10:57.602/ D/LoginActivity: Activity onCreate() instance: com.mypackage.ui.login.LoginActivity@2a9a9e50                                
09-25 16:10:57.602/ D/LoginActivity: Activity onCreate() instance: com.mypackage.ui.login.LoginActivity@2a9a9e50
09-25 16:10:58.193/ D/LoginActivity: Activity onStart() instance: com.mypackage.ui.login.LoginActivity@2a9a9e50
09-25 16:10:58.193/ D/LoginActivity: Activity onStart() instance: com.mypackage.ui.login.LoginActivity@2a9a9e50
09-25 16:10:59.934/ D/MainActivity: Activity instance: com.mypackage.ui.main.MainActivity@3e7f641b
09-25 16:10:59.934/ D/MainActivity: Activity instance: com.mypackage.ui.main.MainActivity@3e7f641b
09-25 16:11:00.605/ D/WalletListFragment: newInstance()
09-25 16:11:00.605/ D/WalletListFragment: newInstance()
09-25 16:11:00.675/ D/ComprasListFragment: Fragment instance: ComprasListFragment{2cbeb452 #2 id=0x7f11079f android:switcher:2131822495:2}
09-25 16:11:00.685/ D/ComprasListFragment: Fragment instance: ComprasListFragment{2cbeb452 #2 id=0x7f11079f android:switcher:2131822495:2}
09-25 16:11:00.685/ D/ComprasListFragment: onCreate(savedInstanceState = new)
09-25 16:11:00.685/ D/ComprasListFragment: onCreate(savedInstanceState = new)
09-25 16:11:00.695/ D/RecargasFragment: Fragment onAttach() instance: RecargasFragment{318ff323 #3 id=0x7f11079f android:switcher:2131822495:3}
09-25 16:11:00.695/ D/RecargasFragment: Fragment onAttach() instance: RecargasFragment{318ff323 #3 id=0x7f11079f android:switcher:2131822495:3}
09-25 16:11:00.715/ D/RecargasFragment: Fragment onCreate() instance: RecargasFragment{318ff323 #3 id=0x7f11079f android:switcher:2131822495:3}
09-25 16:11:00.715/ D/RecargasFragment: Fragment onCreate() instance: RecargasFragment{318ff323 #3 id=0x7f11079f android:switcher:2131822495:3}
09-25 16:11:03.017/ D/StoresMapFragment: Fragment instance: StoresMapFragment{215cf0c5 #1 id=0x7f11079f android:switcher:2131822495:1}
09-25 16:11:03.017/ D/StoresMapFragment: Fragment instance: StoresMapFragment{215cf0c5 #1 id=0x7f11079f android:switcher:2131822495:1}
09-25 16:11:03.047/ D/RecargasFragment: Fragment onStart() instance: RecargasFragment{318ff323 #3 id=0x7f11079f android:switcher:2131822495:3}
09-25 16:11:03.047/ D/RecargasFragment: Fragment onStart() instance: RecargasFragment{318ff323 #3 id=0x7f11079f android:switcher:2131822495:3}  

When I launch MainAcitivity (which hosts a ViewPager with four fragments) I am taking care of also setting the right (?) flags:

protected void startMainActivity(String token) {
    Intent i = new Intent(this, MainActivity.class);
    i.putExtra(PARAM_1, token);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(i);
    finish();
}

I don't use the flag when opening all other activities from Main, since all of them specify MainActivity as the parent, though I don't think it's related.

Yes, I have read and gone through official documentation but still can't figure out what's going on.

What is most strange to me is the fact that this app was built using MVP: each Activity has its own Presenter, View, and Interactor, and the latter only takes care of fetching data from a server. I implemented Retrofit for this, and interactors are not doing this duplication, but Presenters also show duplicated logging.

So basically I have two questions:

  1. Why am I getting double logs?
  2. Am I handling tasks correctly for what I want to achieve? - My gut tells me I also need to specify the taskAffinity so all other activities can be on the "main task"

BTW, I am using Timber and not planting the tree on each Activity but on my App class

Thank you for any possible suggestions.

Upvotes: 0

Views: 211

Answers (2)

Nick Wright
Nick Wright

Reputation: 1413

Do you have any libraries which also plant Timber trees? My app was duplicating log entries when debugging because one of my libraries was also using Timber.

I added the following extra check in my Application class to prevent duplicates:

if (BuildConfig.DEBUG && Timber.treeCount() == 0)
  Timber.plant(new Timber.DebugTree());

Upvotes: 2

Mike
Mike

Reputation: 2705

  1. Based on your logs I assume it is still duplication of logs. Timestamps are equal. I would assume to replace Timber logging with simple Log.d and try if it helps. If so - dig deeper to track when timber duplicates the logs.
  2. At the first glance, everything is correct.

Upvotes: 1

Related Questions