Android go back to activity or recreate - android

I am developing media player application in which i am showing a notification when song starts playing in a service.
Basically i am showing a list view in my first activity(being any one from Artist tab, album tab etc) and on click of an item , player screen is shown.
I have to implement a back button in my player activity, i guess i just have to do a finish() on click of it.
But in case my previous activity is destroyed and i am coming through pending intent(through notification), if i call finish() now the activity is simply destroyed. In such case i want to recreate the previous activity.
Example :
Album > player screen is shown and songs starts in service with status bar notification..
Suppose i press back now and destroy all the activity.
Click on notification > Player is shown again.. if i press back now Album activity should be shown similarly for artist tab.
So far i am just doing:
btnBack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});

I guess you can achieve what you need with setting the intent flags accordingly. The best introduction to these flags I found is the following
Android Activities and Tasks series – Intent flags
It is still a confusing subject for me yet the Android application offered along with this article is very helpful to check quickly what the different flags do.

I thinking you must set more informations about song in Service like a album and when you press back in song activity you should chek service and give back it from Service and init album activity

Related

how do i get calling Activity name if it is a system app ( i.e gallery app )

i have searched a lot but i cant find answer so i thought i should post here.
problem
my app works on images which are share by gallery application(i.e default gallery app) and its works fine. but i want to override OnBackPress() method to check whether back button is pressed or not and if backbutton is pressed then i simply want to get calling activity name and want to kill that activity and go back to home.
i can do this if i call another activity form my activity but how can get calling activity name if it is a system app. (i.e gallery application)
keep in mind that my app called when i share images from gallery app so basically i want to get gallery activity to kill it and can go back home safely. i dont wannna go back to gallery app.
i want to simply go back to home and kill that gallery app instead of going back to gallery app.
here is OnBackPressed() method
#Override
public void onBackPressed() {
// do something on back.
// what i tried so far but it is not working
this.getParent().finish();
this.finish();
}
i tired this and hopefully i can go back home safely but still gallery app run in background. i want to kill calling app too.
public void onBackPressed() {
moveTaskToBack(true);
}
Maybe something like this:
this.getParent().finish();
this.finish();
If this is not working you could try to add a filter in your intent before you open this explicit activity:
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Ensure App Closes when Back Button Press Android

I have an app that opens a web view and uses a javascript interface to play audio on my Android App. I want the app to close if you hit the back button, which should be happening automatically, but it's not.
The only 2 activities I have are the MainActivity and the WebAppInterface. It's possible that the back button is closing the main activity, but the WebAppInterface for the web view (that also plays the audio) isn't closing when the back button is pressed, but I'm not sure.
Any advice? I would post the code, but there's nothing to see here. A note, I never call
finish();
anywhere in the app, and maybe I should?
I want the app to close if you hit the back button
What your users want is for the music to stop when the user hits the back button. Presumably, you will do that by the same sort of mechanism that you used to start the music in the first place.
A note, I never call finish(); anywhere in the app, and maybe I should?
This happens automatically when the BACK button is pressed, assuming you have not done anything to interfere with it (e.g., overrode onBackPressed() and failed to chain to the superclass).
You can can call finish when you start the WebAppInterface from MainActivity.
Like:
Intent i = new Intent(this, WebAppInterface.class);
startActivity(i);
finish();
This way when you call finish on the WebAppInterface, it won't go back to the MainActivity instead it will close like the MainActivity does.

Activity not closing properly

I am calling one activity on click of status bar notification which is having a Complete button. on click of btn. i have folllowing code -
public void completeTask(){
taskDBAdapter.deleteReminder(rowId);
taskDBAdapter.close();
Intent intent = new Intent(this, TaskManagerActivity.class);
startActivity(intent);
finish();
}
whhen i click complete btn new activity (TaskManagerActivity) gets opened properly.But if i reopen my application it still tries to open this activity and not my default landing activity. Any help on this.??
EDIT -
I have tried repositioning my finish() statement . Still its not working.
EDIT 1.1 -
Ok I will provide some details here. Assume my app has two activities
Main Activity
Notification Activity
My app create some notification to display on Status bar. So as soon as i click on status bar Notification actvty will open. Now there is a button called Complete on click of which the code given will fire and main activity (in the code TaskManagerActivity.class) will open. But after I press back button in my app and again reopen it , it opens the notification activity when it should have fired the main activity (as it is launching activity).
Thanks,
Ray
That's the default way android functions. If you press the home button and then open your app again, it will restore the apps previous state (unless it has killed the apps processes and activities due to memory constraints). So you are not actually restarting your app but only restoring it.
If you wanna quit the app, then press the back button. Now when you re-open the app, the original activity will be launched.
Do not modify this behavior. It is the default system behavior and users expect it to work this way. Your app is fine :-)
- First of all the behavior which you are experiencing is the way Android is made to function, moreover when a user gets a call while this app is open, after finishing the app he will definitely want to get back to the state where he left the application.
Still if you want it that way, here it is.....
- Make sure your application has only single instance of it running, by using android:launchMode="singleTask", or android:launchMode="singleInstance"
- Then finish() your Activity at onPause().
#Override
void onPause()
{
super.onPause();
finish();
}

How to maintain activity when user press the home button or back button without stop|exit|submit the activity 2

I have two activity (A, B):
A: Song Button
B(WebView): Song Play List - Embadded from URL
My question is, when user go to B to start play music and user back to A without stop the music (Music is playing in B)...but when user go to B again the WebView in B is new while the old B is still playing the music. So, how can allow user can go to old B to stop music?
I really stuck on this over a week :((
I do appreciate for your ur help.
You can use the following flag on the intent
myIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
while creating activity B. For more information about this, check the link below
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_SINGLE_TOP

Android: Switch between Activities

I am trying to develop an audio player.
My first activity has a list of the mp3's from my sd card and I can choose some them and put them in an ArrayList. I have a button (Add to playlist), and when I press it, it starts the new activity where I take the ArrayList, there I create my player, play music etc. But... when I press the back button on the device(you know this that look like such as a half-circled arrow), I go back to my first activity, but the music don't stop and I can choose again songs and if I press the Add To Playlist button then the app plays two songs simultaneously (my first and this second).
How can I handle this? (I want when i go back, choose or unchoose some tracks and then when i press the button (Add to playlist) to play my new list).
I start my second activity like that:
Intent intent = new Intent (Chooser.this, Player.class);
Bundle b = new Bundle();
b.putStringArrayList("key", plist);
b.putStringArrayList("pos", po);
intent.putExtras(b);
startActivity(intent);
I don't want my player stops, simply I want to add or remove songs in my playlist.
You want to implement the one of the Android Lifecycle methods in your second Activity and tell it to stop the MediaPlayer.
The ones you may be interested in are onPause(), onStop(), and onDestroy().
You can use a global variable to maintain the "is playing" state. If you find the user tries to play a new song and the isPlaying variable is true then stop the player before starting the new song.

Categories

Resources