Solved

Restarting a singleTask activity

Posted on 2013-06-28
12
2,050 Views
Last Modified: 2013-07-09
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
Comment
Question by:brenlex
  • 7
  • 3
  • 2
12 Comments
 

Author Comment

by:brenlex
ID: 39289630
Let's simplify my question -- after using moveTaskToBack to 'hide' a singleTask activity... how can I make it re-appear?
0
 
LVL 35

Expert Comment

by:girionis
ID: 39290047
Try adding these lines to your activity launched

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

Open in new window

0
 

Author Comment

by:brenlex
ID: 39290275
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
 
LVL 12

Expert Comment

by:PCableGuy
ID: 39291918
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
 
LVL 35

Expert Comment

by:girionis
ID: 39292392
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
 

Author Comment

by:brenlex
ID: 39292508
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 35

Expert Comment

by:girionis
ID: 39292877
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
 

Author Comment

by:brenlex
ID: 39293120
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
 

Accepted Solution

by:
brenlex earned 0 total points
ID: 39294017
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
 
LVL 12

Assisted Solution

by:PCableGuy
PCableGuy earned 100 total points
ID: 39295226
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
 

Author Comment

by:brenlex
ID: 39299803
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
 

Author Closing Comment

by:brenlex
ID: 39309774
Found the resolution myself. Some points to PCableGuy for taking the time.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now