سؤال

I have the following code to open the app and once unlocked it shows my app in the foreground. Here is the code:

if (AppStatus.isActivityVisible() == false) {
    PowerManager pm = (PowerManager) mActivity
        .getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP
            | PowerManager.FULL_WAKE_LOCK, "SipManager");
    wl.acquire();
    try {
        Intent it = new Intent("intent.my.action");
        it.setComponent(new ComponentName(mActivity.getPackageName(),
            MainActivity.class.getName()));
        it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mActivity.getApplicationContext().startActivity(it);
    } finally {
        wl.release();
    }
}

This works for me but it starts a new activity. The reason I have FLAG_ACTIVITY_NEW_TASK is because when I use something like FLAG_ACTIVITY_REORDER_TO_FRONT it throws this error:

Calling startActivity() from outside of an Activity context requires FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

Is there a way to pull the activity to the front if it is in background or start a new activity when the application is closed?

I have tried adding setAction(Intent.ACTION_MAIN); and addCategory(Intent.CATEGORY_LAUNCHER); but to no success.

Thank you for your help

هل كانت مفيدة؟

المحلول

You needs just a few more things:

In your manifest.xml, make sure you set your activity launchMode

   <activity
        android:name="com.att.attbusiness.PlanDetailsActivity"
        android:launchMode="singleTop">
    </activity>

then, when you want to get the activity from the stack or start a new one:

Intent i = new Intent(mContext, PlanDetailsActivity.class);
Bundle bundle = new Bundle();
i.putExtras(bundle);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);

You also might need to override OnNewIntent()

   @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    Bundle extras = getIntent().getExtras();
    ...

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top