Bios90
Bios90

Reputation: 811

Android pending intent to resume app, without starting activity

Are there any way top resume app from background to foreground without starting activity?

I have 20 activities in my app. When i receive notification i cant know which activity is top now. Can i somehow resume whole app to foreground (not calling some activity with SingleTop,ReoderToTop flags and so on but just move existing app to foreground)?

Upvotes: 4

Views: 3427

Answers (3)

Andrew
Andrew

Reputation: 1

Recently i have faced with issue I had to resume top activity by clicking on push notification while app was in the background. I found this case close to you)

I resolve this issue by saving top activity intent in onResume() method of BaseActivity, like below:

Intent currentActivityIntent = new Intent(this, getClass());
currentActivityIntent.addFlags(FLAG_ACTIVITY_NEW_TASK);
currentActivityIntent.addFlags(FLAG_ACTIVITY_SINGLE_TOP);
App.setCurrentActivityIntent(currentActivityIntent);

I get top activity intent after clicking on push.

All you need is to get intent of last top activity with both flags: FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_SINGLE_TOP.

Upvotes: 0

David Wasser
David Wasser

Reputation: 95578

Your code is far too complicated. You don't need a dummy Activity. You should just do what Android does when it launches an app from the HOME screen.

Use a "launch Intent" in your Notification:

val intent = getPackageManager().
                    getLaunchIntentForPackage("my.package.name")

If your app is already running, this will bring the app to the foreground without creating any new components. If the app is NOT already running, this will start the app by launching the root Activity.

Upvotes: 4

Bios90
Bios90

Reputation: 811

I found one solution, it may looks weired but it works.

1) Create some dummy activity. This activity will be used as a starting point for pending intent.

class DummyPendingIntentActivity:AppCompatActivity()
{
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
    }
}

2) Made this logic in onCreate of dummy Activity. In this method you can check are there any activities in stack and resume them if needed, or you can restart your app with starting your 'launcher' activity

//Here we check if there any activities in stack
override fun onCreate(savedInstanceState: Bundle?)
{
    if(isTaskRoot)
    {
        //No previous activity running will restart app
        val intent = Intent(this,YourFirstLauncherAcitivity::class.java)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        startActivity(intent)
    }
    else
    {
        //Has some previous activity so will finish to show it
        finish()
    }
}

3) Call this dummy start point in any pending intent and you will resume your app or restart it if needed.

val intent = Intent(context,DummyPendingIntentActivity::class.java)
val pendingIntent = PendingIntent.getActivity(AppClass.getApp(), 777, intent, 0)
...
setContentIntent(pendingIntent)

Upvotes: 0

Related Questions