Check For Activity is onScreen when Notification i

2019-08-03 08:18发布

问题:

I am trying to add upload image feature in my application, using android-upload-service.

I am facing a issue not specific to the library.

When i am clicking on upload notification I want to open a activity using Intent.

code:

val intent = Intent(context, EditorActivity::class.java)
            intent.putExtra("ResourceId", resourceId)//No I18N
            context.startActivity(intent)

But,

  1. First requirement is to start activity if it's not onScreen.(i.e application might be dead, or i might be on different activity)
  2. Second if the activity is already running then do nothing

How can i check is the specific activity is dead or running and on Screen. current code is starting the activity on top of present activity because of what i am having same activity on top of each other.

回答1:

Add launchMode="singleTop" to the <activity> definition in the manifest. That should be enough to do what you want. It tells Android that it should use an existing instance of the Activity instead of creating a new one, if an instance of that Activity already exists at the top of the activity stack in the task.

Please don't use one of the special launch modes singleTask or singleInstance as these usually create more problems than they solve if you do not know exactly what you are doing.



回答2:

First, add android:launchMode="singleTask" for that activity in your AndroidManifest.xml file. This will make sure that there is only one instance of the activity at all time.

Example,

     <activity
        android:name=".activities.MyActivity"
        android:launchMode="singleTask"/>