Prevent duplicate activities - java

So, I'm both new to Java and to creating android apps, but not new to programming. I've read through most of the developer.android.com site, but I haven't been able to find this:
I want to make sure that a certain activity isn't running more than once at the same time. So we have a task somewhat like this:
Activity A) a TabActivity, which launches
Activity B) a ListView that, on-click, opens up
Activity C) which is the interface for a mediaplayer object
Right now, whenever somebody presses the back-button whilst in C (Which is a likely thing, because they're going to listen to a streaming 1-hour long mp3) and then presses another list item, instead of returning to C, C is opened a second time, and two streams are playing. Of course, I'd want only one instance of C running, and I want a second click on the list item to bring C back to the front. This could also be useful for notification intents.
I've been messing around with the flags (especially FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_REORDER_TO_FRONT), but no success so far.
If someone could help me out here I could focus on my next challenge - making it a real feed reader :P
Thanks in advance,

You need to flag your actvity as either "singleTask" or "singleInstance" in your manifest. I can't remember the exact differences between the 2, but either should do what you want. SingleInstance just does something different with the stack.
Example :
<activity android:name="MainActivity" android:launchMode="singleInstance"></activity>
You can handle new calls to startActivity() from the same activity instance with onNewIntent()

I've got it!
For those reading this question and wanting to know the summary: I mistakenly thought more then one activity was running, but it appeared more MediaPlayer instances where running. I made my mediaplayer a class member and am now controlling it from the onStart() event.
I am using SharedPreferences to check if the stream needs to reset and change source, or continue running and just show the interface.
Thanks for all your reactions. Really helped me out.

Just Edit the package names in .xml
EX: com.org.MainActivity
change to
.org.MainActivity
It works for me.....yup

Related

Android finish a certain activity instance, regardless if its single or multiple

I am wondering if there is a good approach to finishing an activity with multiple instance. I made that behave w/ multiple instance (default) launchMode on purpose.
For example I got Activity A and I go to Activity B. There is a Chance for me to do these scenarios:
Scenario 1 = A > B > B > C and I want to destroy all instances of B only
Scenario 2 = A > B > B > C and follow the current back stack no problem
But for Scenario 1, I need to retain the state of A (In Activity A this is the selected Tab state scrolled in this item etc etc.)
What have I tried so far:
I tried modifying launchMode in manifest, its not what I need
Tried using finishAffinity() but it destroys all running activities
Tried using Intent with CLEAR_TOP or NEW_TASK but it resets my Activity A state
finishActivity(requestCode)
Browsed some previous questions here in StackOverflow regarding this but no luck for me.
The scenarios are solved using if else statement and I managed to solve the problem just by calling finish() twice but is there any other way around it? Its kinda messy having to call finish() twice along with a boolean checker.

Send String Data without Changing Activity(Intent)

I am trying to send the string from one Activity to Another WITHOUT changing the CURRENT ACTIVITY. This my code I used:
Intent intent = new Intent(this, AnotherActivity.class);
intent.putExtra("getFollowerNumberData", txt);
startActivity(intent);
Using this code bring me to the other activity or I do not want that. I just want to send this string without changing activity. I tried this one
Intent intent = new Intent();
But it is crashing my app. First of all it is possible to do so? If yes, how can I achieve that?
Edit: To be more clear, it works like follower and Following features in other apps or games. When you follow someone the number of the person you just followed goes up as well your Following number. Since my SignInPlayerProfile.Class(where the Following should go up to one too) is in another activity I was trying to get this information intent.putExtra("getFollowerNumberData, txt); from Main Activity and display that in the SignInPlayerPlayer by using text.setText(). But the Problem is by using intent = new Intent(this, AnotherActivity.class); it brings me to SignInPlayerProfile Activity which I do not like since
I am trying to send the string from one Activity to Another WITHOUT changing the CURRENT ACTIVITY(or simpler words WIHTOUT Going to the Other Activity).
Thanks for Help.
When an activity getting closed, according to it's life-cycle, it will be destroyed and no longer exists, when you start an activity it will be created and then it can get your data, so you can't send data to an destroyed activity which no longer exists
You can use Static Variables to communicate between activities (Classes), you can change value of a Static-Variable of an activity from another activity, but it's not a good option for data you need to be alive because Static Variables lives on Heap Memory and Heap will be freed if Android OS needs more Memory
Another way is to create a Message Handler in your first activity as a Static Variable and then send a Message to the Handler from second activity, see this example :
http://stacktips.com/tutorials/android/android-handler-example
I suggest you using SharedPreferences for saving your data in first activity and load from it on second activity
EDIT :
According to your edit, the "Number" you want to use in another Activity as "Following" or "Followers" is just needed when the second activity is visible, you should use sharedpreferences to save the "Number" and load from it when you need it. For example before text.settext() method you can load the number from sharedpreferences and then pass it to text.settext()
You should not save your data on the variables or classes and should save them on a file like a Database or SharedPreferences then you can load them every time you want
Furthermore you can search about Activity life-cycle and see how to use life-cycle events like OnStart to load your data
Firstly please consider startActivityForResult() ,we can send information from one activity to another and vice-versa. As mentioned consider using shared_preferences for local in memory storage. To truly accomplish this feat in a elegant way though, do consider using obervables(rxandroid). You publish observations in one activity or fragment ,
then subscribe in another activity or fragment. I did not mentioned event bus nor otto since rxjava/rxandroid surpasses it. They act a promise context management system. Also because observable in process dependent consider using broadcast receivers, to broadcast events through out your application and external if so desired.

Does Android Keeps History of Changes in Views?

Does Android Keeps History of Change in (View or Activity) same as any Log file! and Can we use that information occurring to our requirement? I want to set Some flags on the base of that information for future purpose in application!
Your question is too broad. There can be different kind of history: history of touches, history of text editing, history of view location, history of started activities, history of started applications etc. But Android only keeps what is important for Android users. For instance, it keeps list of recently launched applications, or activities in a task stack. But this is not really a history. There is also no history of view changes or so.
Answering your question - no. Most likely you won't find anything, what you can reliable use as a history. If you need the real history, you need to set corresponding listeners and implement it by your own.
The answer is Yes, By default Android keeps the History for the Activity if the Flag is not set. You can set this Flag through Intent by this code.
Intent i = new Intent(); i.setFlags(|Intent.FLAG_ACTIVITY_NO_HISTORY );
Flag Description
If set, the new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished. This may also be set with the noHistory attribute.
Check this link for further information about the FLAG used in android Intent.
Hope this helps.

Empty (or fill) Recent Apps List

I have developed a baby app where I need to lock all keys and ways out of the app, in order to be able to hand over the phone to my daughter. I used the "replace home screen" method to capture the home key, and that works great. All other hard keys (except power button, but that's not an issue since it's not easily pressed anyway) are locked. But the often discussed long press of home key is still launching the recent apps and offers a way out (which of course the daughter finds almost instantly).
I have seen other apps in market (toddler lock and more) work around this by emptying the recent apps list (or filling it with an icon that leads back to the baby app), but I have no idea of how to accomplish this. Anyone have a solution? How do I fill the recent apps from my app? Toddler lock even resets the recent apps after the lock-app is exited.
You could create an activity in your application that does nothing, and start it with an intent with the FLAG_ACTIVITY_NEW_TASK flag set. Fire this intent however many times you need to fill the history (my Incredible has 8 spots). For repopulating the history, look into ActivityManager.RecentTaskInfo.getRecentTasks (int maxNum, int flags)
See Documentation
It looks like you need to hold the GET_TASKS permission
The intents that originally fired those apps are stored as baseIntent, so you should be able to store them yourself(don't forget the extras), and re-fire them in order to repopulate the history list.
This is a hack, but it should work
Edit: After playing with this a little, it looks like you would need to have 8 different activities for this method to work, which is not realistic.
You need to do what is described in Mobius's post, but also set android:taskAffinity="" on the dummy activity elements in the android manifest file.

How to implement an Android detail notification?

This has probably been asked before, but I can't find a good way of implementing it. I'm trying to write a program that manages a form of messages, and these messages are received from an external data source. This all works. However, the problem comes when I try to notify the user: I would like to have the notification jump directly to the message when it is touched, but this seems to mess up the back stack. This is probably best explained by example:
I open up the message list, the main activity, and browse for a while.
I hit home and go into another app (let's say Music).
A new message is received. A notification comes up, which I touch. The message detail view is displayed.
Now I hit Back. What I want to have happen is that I return to Music, but unfortunately, Back sends me to the message list, and then hitting Back will return me to music.
The both the list and the detail activities are marked as "singleTop", and the exact flags that I use for the notification Intent are:
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
I figure if the Messaging application can do this, why can't I?
I have found one way to do this, but it's still not ideal:
Change the detail activity to have a different task affinity from everything else.
Add android:launchMode="singleTop" and android:excludeFromRecents="true" to the detail activity's manifest entry.
(Optional) Modify the list activity to open the detail activity with FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET. (This makes the task more like the built-in messaging app.)
The only fault to this scheme is that switching back over to the app will always go back to the list activity, but at least it is consistent. If someone else has a better way to do this, I'd love to hear it.

Categories

Resources