1

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.

Anmol
  • 8,110
  • 9
  • 38
  • 63

2 Answers2

1

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"/>
George Arokiam
  • 262
  • 2
  • 9
  • @Override protected void onNewIntent(Intent intent) {} i have to write above in my activity? – Anmol Sep 04 '18 at 16:14
  • 1
    You do not need to override `onNewIntent()` because the default implementation of `onNewIntent()` does nothing. Secondly you do not need to use `singleTask` launch mode. That is a special launch mode and usually causes more problems than it solves. – David Wasser Sep 04 '18 at 21:07
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.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Can you care to explain what kind of problems would singleTask bring that singleTop wouldn't? – George Arokiam Sep 07 '18 at 14:51
  • In a comment there are limited characters. I could write a book about this. Basically, `singleTop` is a standard launch mode. `singleTask` and `singleInstance` are special launch modes, usually only required in very special circumstances (for example, if you are writing a HOME-screen replacement). `singleTask` has the problem that it conflicts with `taskAffinity` and so it doesn't always do what it should if you don't understand the relationship with `taskAffinity`. If it does what it should you can end up with 2 tasks for your app instead of 1. If you don't provide different icons and ... – David Wasser Sep 16 '18 at 12:30
  • ...names for the different tasks, it can confuse the user as he doesn't understand why half of your app is in one task and the other half in another. Also, if your `Activity` can be launched by another application, that adds another layer of complexity which can make it difficult to return to the application that launched your app, etc. It is really best to just stay away from these special launch modes unless you know exactly what you are doing. – David Wasser Sep 16 '18 at 12:32
  • `singleTop` is easy. It doesn't create any new tasks. If you launch a `singleTop` `Activity`, Android will create a new instance ONLY IF there isn't already an instance at the top of the `Activity` stack in the target task. This allows you to reuse an existing `Activity` instance if it is already the topmost `Activity` in the stack. – David Wasser Sep 16 '18 at 12:34