Pergunta

The constructor of a pending intent needs an Intent object in it from the current context to the next activity. In my app, I have only a single activity containing multiple Views. No Second Activity.

The Views are Destroyed or made visible on demand. Obviously I cannot add a View to the constructor of an intent. So how shall I direct the pending intent? Multiple views have been accommodated into the main.xml using the <include/> command.

Foi útil?

Solução

This is very easy to accomplish. mvnpavan already gave you good hint, you just need to do some additional work. First of all you should get familiar with this page http://developer.android.com/guide/components/tasks-and-back-stack.html. It explains how to manage your application tasks and back stack. In your case you have 2 options:

1.Configure launchMode property of Activity in manifest. Setting singleTop should do the job since your are using just one activity.

 <activity
        android:name="com.yourpackage.YourClass"
        android:launchMode="singleTop">
 </activity>

2.Add flag FLAG_ACTIVITY_SINGLE_TOP to your Intent(the one you are passing to PendingIntent):

Intent intent = new Intent(this, YourActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

Please note that in both cases you can implement callback method of you activity onNewIntent(). Use it in case you wish to do some extra work when app is bring back to the foreground.

Update:

Regards keeping connection to the server I would use Service, not Activity. Then you can do whatever you like with your activity. This is the best practise. If you worry that Activity has no direct access to the Service API, you can bind to that Service. Have a look at this page http://developer.android.com/guide/components/bound-services.html You can just copy/paste the code and your are done. It's very easy to maintain. In the future you may need to create another activity which require connection to the server. It will save you a lot of time and headaches :).

Outras dicas

Use pending intent to resume your application and not recreate it like this:

Intent i = new Intent(this, youractivity.class);
pIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

Hope it helps! ;)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top