Search code examples
androidandroid-activityandroid-webviewandroid-service

Activity restarts when called from service


I have a service in my app of a floating button (like facebook messenger) and when clicked , my activity returns from the background , but it doesn't return in the same way it was when it was minified , it starts over calling onCreate once more , any idea what causes this behavior ?

In my manifest the acivity is with property :

            android:launchMode="singleTask"

and it is called by my service like so :

    Intent i = new Intent(FloatingViewService.this, DrawerActivity2.class);
    i.putExtra("fromController",true);
    i.setFlags(FLAG_FROM_BACKGROUND);
    FloatingViewService.this.startActivity(i);

My main problem is inside the activity i have a webview and it's reloading everytime the button is clicked and calls the activity instead of just staying the same way.

After the update this is my activity in the manifest

        <activity
        android:name=".DrawerActivity2"
        android:configChanges="orientation"
        android:launchMode="singleTop"
        android:screenOrientation="portrait"
        android:theme="@style/OtherTheme"/>

UPDATE 2

It might be worth to mention , it's a library that sits in an app.

I tried this code for opening the activity :

Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName()); startActivity(intent);

and it opened the first(of the original app) activity and not the one that is open.

any help will be appreaciated


Solution

  • I could only imagine what your FLAG_FROM_BACKGROUND means. And I wonder if it called so cause you had some crashes before with something like "activity could be launched without FLAG_NEW_TASK from applicationContext bla bla". Whatever.

    So if you have there Intent.FLAG_ACTIVITY_NEW_TASK you will always get your DrawerActivity2 instantiated in NEW_TASK. It will always be, you know "new one" activity. And changing launchMode to singleTop won't give expected result. Read about launchModes and Flags here

    Note you're not allowed to launch Activity within same TASK with an ApplicationContext(or BaseContext/ContextWrapper that Service extends). For that purpose you have to use Context of an Activity for starting new Activity

    And about Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName()); startActivity(intent); Probably it works as expected

    The current implementation looks first for a main activity in the category Intent.CATEGORY_INFO, and next for a main activity in the category Intent.CATEGORY_LAUNCHER. Returns null if neither are found.

    Android developers source

    And you ask

    my activity returns from the background, it starts over calling onCreate once more , any idea what causes this behavior ?

    My assumption is FLAG_FROM_BACKGROUND:

    Intent i = new Intent(FloatingViewService.this, DrawerActivity2.class);
        i.putExtra("fromController",true);
        i.setFlags(FLAG_FROM_BACKGROUND); /*is it really Intent.FLAG_ACTIVITY_NEW_TASK ???*/
        FloatingViewService.this.startActivity(i);
    

    How to fix it ? Probably to startActivity with ActivityContext instead of ApplicationContext. But this is different story to tell. And can't recommend a "good" way of how to implement it.