changing intents within the same activity Android - android

I am trying to edit player on the same layout as creating a player.
When i press the edit player button , I change the headings of the create player page to "Edit Player" and populate the listbox with the player name. I would like to know how i can return to the create player activity when i press done. Thankyou

Normally you would call finish() to finish the current activity, and the user should be redirected to the first activity on the stack, which apparently should be the Create Player activity.
So, set a click listener for the done button, an in onClick call finish()
Or, you may start the Create Player activity in onClick, like in you would start any regular activity:
Intent intent = new Intent(EditPlayer.this, CreatePlayer.class);
startActivity(intent);

When i press the edit player button , I change the headings of the
create player page to "Edit Player" and populate the listbox with the
player name.
Seems like you did all actions in one activity?
Better to create two activity if you want to switching. creating a player A activity -> edit pressed -> startActivity B -> editing -> done and finish() activity B -> resume A activity.

Related

How to update a variable in Activity A from Activity B, every time the variable changes?

Image description of the problem
I have a music player app. Activity A is my main menu. You choose a song from a list in Activity A and that takes you to the Activity B with intent(passing data like songTitle, songArtist etc).
When I press the back button when I'm in Activity B, it takes me back to Activity A with intent(passing data like songTitle, songArtist etc). I show the currently playing song title and artist name in Activity A at the bottom.
My problem is that if I'm in Activity A and then the song changes AUTOMATICALLY to the next song in ActivityB, the song title and artist name doesn't change in Activity A because I didn't pass any intent from Activity B to A.
I tried to use SharedPreferences in onStart() but the changes in text doesn't apply unless I press the home button on the device and come back again to the Activity A.
How can I listen the variable changes in another activity and update my TextViews accordingly?
you have two options here. you can transform those activities to fragments and then you will be able to share data with activity scope view model I believe thats good option and you should do it like that, but if for some reason you have huge codebase and it will be very hard to make those activies fragments you could just add onActivityResult in ActivityA and return current music id or something from ActivityB.
You should look into LiveData
LiveData is on observable data class. You can find more about it from here:
LiveData Android
I have solved my problem with SharedPreferences.

exit application from fragment activity

I want to close the android application from fragment activity. Fragment layout contains two menus.one of the fragment will display the list. When we click on the list it will display the details for list in Dialog Alert that alert box contains two buttons.if you click one button it will open another activity that activity will contains list. if we click the list it will open previous activity in the fragment activity.if you click back button from this activity it is not closing the app completely.
please give me good solution to exit the app from the situation
As i understand you activity managment is something like this:
A -> B -> A
And if you pressing back now it is not closing.
You should watch to Intent.FLAG_ACTIVITY_CLEAR_TOP
So when you opening Activity A again you code should look like this:
Intent a = new Intent(this,A.class);
a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(a);

How to kill all activity when pressing back key at android?

There are two activities, Start Activity is composed VideoView and Main Activity(listview) overlays on VideoView. Because I don't know how to overlay Listview transparently on videoview at one activity. That's why I used two activity.
StartActivity(videoView) -> MainActivity(Listview)
The problem is that when I finish this app using back key, only ListView is killed. So I have to press back key again for killing videoView. I have searched all info, but I can't find out.
when you go to Listview from videoview, at that time use finish() with intent. just like below
Intent mIntent = new Intent(Activity_Listview.this,
Activity_videoview.class);
startActivity(mIntent);
finish();
What you probably want to use ("to overlay the listview") is a fragment.
Since your app will only use 1 activity it will exit immediately when the back button is pressed. This would be the correct way to do the thing you described instead of trying to kill 2 activities with 1 back-key press.
When launching MainActivity(Listview) you could use startActivityForResult (http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int) ) and in the setResult method you can add some information which then to trigger the finishing of StartActivity(videoView).
Please note that these are only tricks. The correct implementation would be to use Fragments (as user3249477 suggested).

Android go back to activity or recreate

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

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