1

im working on app that require launch app from a service. my problem is service create new instance of app Although my app is not closed. i want just one instance. i find this question here like my problem but i can't use Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT cause my apps crashes.

i change my tabactivity(main activity) to

android:launchMode="singleTop"

but problem still remains. would some one guid me. thank you very much

this is my service:

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;


    public class MyAlarmService extends Service {

         @Override
            public void onCreate() {


            }



            @Override
            public IBinder onBind(Intent intent) {
                return null;

            }



            @Override
            public void onDestroy() {

                super.onDestroy();


            }



            @Override
            public void onStart(Intent intent, int startId) {
                super.onStart(intent, startId);
                openActivity(AlertActivity.class);
                Log.i("LOG", "service started");

            }



            @Override
            public boolean onUnbind(Intent intent) {
                return super.onUnbind(intent);

            }



            public void openActivity(Class activity) {

                Intent i = new Intent(getBaseContext(), activity);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);

            }
    }

and this is my manifest:

<application
        android:icon="@drawable/drink1"
        android:label="@string/app_name" 
        android:name=".G"
        >
        <activity
            android:label="@string/app_name"
            android:name=".MainTabActivity" 
            android:launchMode="singleTop" >
            <!-- it cause to just one instance of app be exist -->

            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".AlertActivity" android:theme="@style/Theme.Transparent">


        </activity>
        <activity android:name=".DrinkoflifeActivity"
            ></activity>

        <activity android:name=".SettingsActivity">


        </activity>

        <activity android:name=".StaticActivity">

        </activity>

        <activity android:name=".ContactUsActivity"></activity>
       <activity android:name="org.achartengine.GraphicalActivity"/>
        <activity android:name=".DataActivity">


        </activity>
        <activity android:name=".LogoActivity"></activity>
        <activity android:name=".SplashActivity">


        </activity>

        <service android:name=".MyAlarmService" />

    </application>
Community
  • 1
  • 1
Mahdi
  • 6,139
  • 9
  • 57
  • 109

1 Answers1

0

Setting launchMode="singleTop" on your MainActivity is probably not necessary and isn't going to help you here.

When your service starts AlertActivity, it will create a new instance of that activity. If your application has an active task, this will also result in the active task being brought to the foreground and the new instance of AlertActivity will be created on top of any other activities in the task. In a comment you wrote that AlertActivity leads to MainActivity then the problem is in how you are starting the MainActivity from the AlertActivity.

You should probably start the MainActivity from the AlertActivity this way:

Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);

Assuming that the MainActivity is the root activity in the task and there is still an active instance (ie: it has not yet been finished), this code will remove any other activities from the task stack and go back to the existing instance of MainActivity.

David Wasser
  • 93,459
  • 16
  • 209
  • 274