[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2371
  • Last Modified:

Restarting a singleTask activity

I am having some trouble with bringing a singletask activity back into view.

Manifest:

<activity
    android:name=".test.MyActivity"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:icon="@drawable/my_launcher"
    android:label="@string/title_activity_my"
    android:launchMode="singleTask"
    android:taskAffinity=".myTask"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Black.NoTitleBar" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Open in new window


MyActivity Code:

Intent i = new Intent(getApplicationContext(), MyActivity.class);
i.putExtra("blah", stuff);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK); // I have tried almost every flag combo here!
startActivity(i);

Open in new window

Scenario:

Upon onBackPressed() the activity calls moveTaskToBack (which hides my activity). Upon receipt of a specific incoming event, the above intent code is called, however NOTHING happens!?! No OnNewIntent() fired, no onCreate fired...

HOWEVER: If I also add a separate PendingIntent to a Notification object, it WORKS!?

    Intent notificationIntent = new Intent(getApplicationContext(),MyActivity.class);
    notificationIntent.putExtra("blah", stuff);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notif = new Notification(icon,text,when);
    notif.flags |= Notification.FLAG_ONGOING_EVENT;
    notif.setLatestEventInfo(getApplicationContext(), contentTitle, contentText, contentIntent);
    m_notifMgr.notify(1, notif);

I don't understand why it works from a PendingIntent (firing onNewIntent), but not from a direct startActivity call.

Please advise. Thanks.
0
brenlex
Asked:
brenlex
  • 7
  • 3
  • 2
2 Solutions
 
brenlexAuthor Commented:
Let's simplify my question -- after using moveTaskToBack to 'hide' a singleTask activity... how can I make it re-appear?
0
 
girionisCommented:
Try adding these lines to your activity launched

i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);

Open in new window

0
 
brenlexAuthor Commented:
No difference.

I have simplified matters and placed the same logic into a small test app:
Activity 'A' = singleTask, with two buttons: btnStartTimer to start a timer, and btnMoveToBack to move 'A' to back.  When the timer expires, the intent to create 'B' is called.
Activity 'B' = plain 'hello world' screen.

Results:
(i) If I start the timer via btnStartTimer click, and do not press btnMoveToBack, then the intent is created and 'B' appears.
(ii) If I start the timer via btnStartTimer click, and DO press btnMoveToBack, then the intent is still created and 'B' does NOT appear -- logging shows Activity 'B' onCreate NOT called.

Which raises the question -- can I actually get intents to be processed from an activity that is not shown?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
PCableGuyCommented:
Hi  brenlex,

Just wanted to make sure I understand, so I have a few questions:

1)  Hidden Activity A (hidden by the Android native method Activity.moveTaskToBack(boolean) ) is supposed to receive an event message. After receiving the event message, Activity A will start Activity B?

2)  What kind of message is Activity A waiting for, a system message, a message from your App, or a message from another App, what's the nature of the message?
0
 
girionisCommented:
brenlex when another activity comes into the foreground (lets say activity B) the previous activity is paused (the onPause method is called). Lets call this previous activity A. The activity B that comes into the foreground will not be created until the onPause method of activity A returns.

Where do you have the code that starts the event? As an experiment try to put it in the pause method of activity A and see what happens.
0
 
brenlexAuthor Commented:
I have taken a step back and thought about this... when using moveTaskToBack, Activity 'A' actually becomes a paused activity -- is it actually possible to launch intents from a paused activity?

Background to actual app requirement (as opposed to the test app):
- With regard to the actual app, there is no Activity 'B' equivalent.  I was just trying to make Activity 'A' show itself again when the event occurs.  It was only when I was failing to get the Intent to work that I started testing with a secondary ('B') Activity.
- The actual app uses a third party API which triggers a callback event, which resides on the singleTask Activity 'A'.  On occurrence of this event I just wanted to 'show' Activity 'A' again, but have been failing (hence this post).

Presently my actual app behaviour (as indicated on initial post) is that I create a PendingIntent to a Notification object, which notifies the user of the event, allowing them to bring Activity 'A' back into view by pressing on the notification drawer entry.  This is fine, but I also want Activity 'A' to come back into view when the event occurs, without the necessity for notification bar user interaction.
0
 
girionisCommented:
but I also want Activity 'A' to come back into view when the event occurs, without the necessity for notification bar user interaction.

Then have activity B listening for this event and when it occurs it can bring activity A to front.
0
 
brenlexAuthor Commented:
There is no Activity B. Besides, the point is, there is no Activity on the screen when the event occurs -- which is my problem... the event fires OK on a (singleTask) paused activity and fires the startActivity code... but nothing further happens.
0
 
brenlexAuthor Commented:
I finally found a method that works.  As it appears, an intent (with any consequence) cannot be fired from a paused (singleTask) Activity. Therefore my only option was to implement an IntentService (utilising Intent flags FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_NEW_TASK) which in turn creates the required intent to 're-show' my singleTask Activity.
0
 
PCableGuyCommented:
Looks like you are on the right track by re-thinking it and using a Service. If you haven't read about the Activity life cycle already, here's a good explanation: http://developer.android.com/guide/components/activities.html

What stands out to me is that a Paused Activity can be killed by the Android OS, so it seems counter intuitive to Pause an Activity and then hope to resume it within its own code.
0
 
brenlexAuthor Commented:
It all works perfectly fine through use of IntentService with a singleTask.  

I guess the next time I will be tossing any event generating APIs onto a dedicated service with a host of Intents, with Activities solely as the GUI view into the app.

Live and learn.
0
 
brenlexAuthor Commented:
Found the resolution myself. Some points to PCableGuy for taking the time.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now