How to restart activity using intent? - java

I am making an android app which plays mp3 files. I am launching the mp3 playing activity from within another activity using intent:
Intent intent=new Intent(ListViewA.this,mp3player.class);
intent.putExtra("feed",gh);
i.setFlags(0);
i.setPackage(null);
//intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
Now when the user selects another song from a list, I want to close the previous instance of the mp3player activity and start a new one using the intent code (above). How do I go about doing that? Thanks.

Instead of startActivity use startActivityForResult(intent, PLAY_SONG) then you can call finishActivity(PLAY_SONG) where PLAY_SONG is a constant number.
class member
private static final int PLAY_SONG
And then
finishActivity(PLAY_SONG);
Intent intent=new Intent(ListViewA.this,mp3player.class);
intent.putExtra("feed",gh);
i.setFlags(0);
i.setPackage(null);
startActivityForResult(intent, PLAY_SONG)

Hey its very simple Instead of calling Intent from Mp3Playeractivity call finish() when you are pressing the back button by implementing
#Override
public void onBackPressed(){
finish();
}
which will cause close your MpeplayerActivity

Related

Open Activity on notification button click when app is closed

I'm trying to open the MainActivity when the user clicks a button in my notification, while the app is only running in the background with a service. When the button is clicked, these lines are triggered in the Service class:
Intent openApp = new Intent(this, MainActivity.class);
openApp.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(openApp);
I've checked it, and the lines are triggered, so there's no problem in reacting to the button's click, the Activity won't open though.
Any suggestions? Why isn't this working for me and how can I make it work?
Edit
I was asked for some more code, so in my onStartCommand() inside my Service, if it starts with a stop-action within its intent, I call the killService() method, which kills the Service, starts the MainActivity and do some other stuff:
if (action != null && action.equals(ACTION_STOP_SERVICE)) {
killService();
}
To set the Notifications button, I use this code:
Intent stopActionIntent = new Intent(this, TimerService.class);
stopActionIntent.setAction(ACTION_STOP_SERVICE);
PendingIntent stopActionPendingIntent = PendingIntent.getService(this, 1, stopActionIntent, PendingIntent.FLAG_IMMUTABLE);
timerNotificationBuilder.addAction(R.drawable.stop, "Stop", stopActionPendingIntent);
And as I said, the button already reacts to the user clicking on it, so that's not the problem.
You can try to receive the click in a BroadcastReceiver and then open activity from there.
Try this to add a action button o your notification:
timerNotificationBuilder.addAction(createNotificationActionButton("STOP");
Where the createNotificationActionButton method is this:
public NotificationCompat.Action createNotificationActionButton(String text){
Intent intent = new Intent(this, StopwatchNotificationActionReceiver.class);
#SuppressLint("InlinedApi") PendingIntent pendingIntent = PendingIntent.getBroadcast(this, new Random().nextInt(100), intent, PendingIntent.FLAG_IMMUTABLE);
return new NotificationCompat.Action(0, text, pendingIntent);
}
Create a class named StopwatchNotificationActionReceiver and make it extent a BroadcastReceiver`. This is the code for that class:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class StopwatchNotificationActionReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
PrefUtil.setIsRunningInBackground(context, false);
PrefUtil.setTimerSecondsPassed(context, 0);
PrefUtil.setWasTimerRunning(context, false);
context.stopService(MainActivity.serviceIntent);
Intent activityIntent = new Intent(context, MainActivity.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActvity(activityIntent);
}
}
Also you need to register that receiver in your manifest like this:
<receiver android:name="StopwatchNotificationActionReceiver"/>
Where the MainActivity.serviceIntent is a public static variable which looks like this:
public static Intent serviceIntent;
And this intent is only used to start the service like this:
//In onCreate
serviceIntent = new Intent(this, TimerService.class);
//In onPause
PrefUtil.setTimerSecondsPassed(this,seconds);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(serviceIntent);
}
Or you can try the simple method:
if (action != null && action.equals(ACTION_STOP_SERVICE)) {
Context context = this;
Intent activityIntent = new Intent(context, MainActivity.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActvity(activityIntent);
killService();
}
Edit
Another solution is here. Again. You need to refer to my repo as I have made changes to the files in order to complete your task. In the service class, refer to this method. There, I start the activity if the action is reset(r). Or else, it opens the broadcast receiver. Then, in the activity, I receive that extra in the onResume() method. If the reset button is not clicked, it opens the Receiver class.
And as always, you can view the result of the app from here.
I hope that code will do your work.
I found it! See this answer.
This answer suggests enabling ScreeanOverlay settings because as of Android 10 and later you can no longer open an activity from the background just by calling the lines I've used.
To make it work, you'd have to add this permission through your Manifest.xml:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
And then the user would have to enable the Display over other apps setting.
I searched for an option to get the user to this setting more easily and found this answer.
This answer gives a code that redirects the user to the Display over other apps setting
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 0);
}
and then I guide the user with the notification's content (text) on how to enable the setting.
Once the setting is enabled, The lines I've used before work.\
So, problem solved?
Not Completely Solved
this whole configuration described above works, but only if the app is not killed.
If the app was killed and I try the method listed above, the app joins the recent apps list, but won't open and show up.
A solution that solves this issue as well will be accepted as an answer instead of this one.

How to switch Activities without killing them in Android?

I have this Activity1(Restaurant) which switch to Acticity2(Home)
Intent intent = new Intent(RestaurantActivity.this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish(); // Call once you redirect to another activity
My other Activity (Home) has a boolean to check if I have been at Home before
public static boolean visitedHomeAlready = true;
if(!visitedHomeAlready) {
Intent intent = new Intent(HomeActivity.this, RestaurantActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish(); //
}
My idea is: If I have not visited the home before and switch to Restaurant Activity, I want to "remember" the data in the Home Activity to continue my game after come back home but it always start from the beginning from my home...
I am not sure which flags I have to set or which method to resume that state from where exactly where I switch the activity.
As Amin commented above,the finish(); means that the first activity has finished its job and the app will end it,if you removed it the second activity will start,but the first activity will still be in its place and you can go back to it just by clicking the back button on your phone or virtual device.

cannot understand the finish method android [duplicate]

This question already has answers here:
Is it a good idea to call finish() after starting a new Activity in Android?
(3 answers)
Closed 5 years ago.
protected void onCreate(Bundle savedInstanceState) {
//if user is already logged in open the profile activity directly
if (SharedPrefManager.getInstance(this).isLoggedIn()) {
finish();
startActivity(new Intent(this, Home.class));
}
buttonSignIn.setOnClickListener(this);
buttonSignUp.setOnClickListener(this);
}
Can someone please explain to me why is finish() called before launching the Home.class if the user is already logged in. I'm trying to go through some source code and not able to understand this.
simply when you call finish();
its more like an intent which do this :
Intent intent = new Intent(whereYouareActivity.this , mainActivity.class};
startActivity(intent);
when you finish an activity, the activity gets destroyed but not the process. This is an important aspect of Android programming and is important to understand how it's different from other platforms.
And you can finish your activity by adding these lines :-
if (SharedPrefManager.getInstance(this).isLoggedIn()) {
startActivity(new Intent(this, Home.class));
finish();
}
After finish() you can't get context from the activity.
You have to start activity like this:
if (SharedPrefManager.getInstance(this).isLoggedIn()) {
startActivity(new Intent(this, Home.class));
finish();
}
By calling finish() you actively close your current Activity. This prevents your activity from being shown again when the user presses the back button.

build a "fake" stake using startActivity()

I need to build a fake back stack in my application. I am starting an activity using aContext.startActivity(aIntent) and would like to build a stack to allow the user to go to the HomeActivity instead of exiting the application.
Now I know about the TaskStackBuilder but I am not sure on how to implement it when it comes to startActivity method.
This is what I got so far when building the stack but I am not sure how to use it in startActivity method
Intent detailActivity = new Intent(aContext, DetailsActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(aContext);
stackBuilder.addNextIntentWithParentStack(detailActivity);
PendingIntent pendingIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Why don't you just use onBackPressed
Context context = this;
#Override
public void onBackPressed()
{
Intent intent = new Intent(context,YourActivity.class);
startActivity(intent);
}
You need to start activity without adding to back stack, then override the onBackpressed
//Start activity without adding to history stack
Intent i = new Intent(...); // Your list's Intent
i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY); // Adds the FLAG_ACTIVITY_NO_HISTORY flag
startActivity(i);
//Override the newly open activity onBackPressed()
#Override
public void onBackPressed()
{
Intent i= new Intent(getApplicationContext(),HomeActivity.class);
startActivity(i);
}
The fix that i found to work without having to use onBackPressed was to add a flag to the intent
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
For building fake stack use TaskStackBuilder.This will help you to create back stack.
Suppose you activities are 'ActivityA', ActivityB, ActivityC, ActivityD. Now you are in ActivityB and from ActivityB you want launch ActivityD and want a back stack like this:
ActivityA -> ActivityC -> ActivityD
That means back press from ActivityD will go to ActivityC and back press from ActivityC wil go to ActivityA
In ActivityB you can use this
TaskStackBuilder.create(this)
.addParentStack(ActivityA.class)
.addNextIntent(new Intent(this, ActivityA.class))
.addNextIntent(new Intent(this, ActivityC.class))
.addNextIntent(new Intent(this, ActivityD.class))
.startActivities();
To use TaskStackBuilder you min sdk version should be 16 or higher.
For sdk less than 16 you can manually check in onBackPressed and start Activity.
In Activity D:
#Override
public void onBackPressed(){
Bundle extras = getIntent().getExtras();
if (extras.containsKey("FROM_ACTIVITY_B_FOR_STACK")){
// start Activity C
}else{
super.onBackPressed();
}
}
Do this for other activities where you want to add back stack

Communication between running activities?

I'm new to android and is currently developing a quest/tresurehunt application. It is a prototype and is going to be evaluated in the field and i would therefore like to implement some sort of cheat to skip checkpoints if the application should crash during the tests.
My application work the way that when you select a quest a activity will be loaded to handle all checkpoints and the user location(ActivityAdapter.java). This activity will open the diffrent navigation tool activities and pass information to them based on the next checkpont using intent. I have implemented a long press event in the app i would like to activate the skip/cheat. My problem is that i can't figure out how to do this.
Location changed event of ActivityAdapter.java:
public void onLocationChanged(Location location) {
location.distanceBetween(location.getLatitude(), location.getLongitude(), lat, lng, dist);
if (dist[0]<= 10) {
if (cid == checkpoints.size()) {
Intent intent = new Intent(ActivityAdapter.this, SuccessActivity.class);
intent.putExtra("title", checkpoints.get(cid).get("title"));
startActivity(intent);
} else {
new loadAndStartQuest().execute();
}
}
}
loadAndStartQuest() just find the next checkpoint and start the right activity (navigationtool). I have tryed creating a object of the ActivityAdapter and set a variable and add it to the the if statement which did not work. I guess it is because it will create another instance of the activity and not affect the current/running one. So how would you communicate between two running activities?
To send data from Activity1 to running Activity2, you must pass through the next steps:
In Manifest.xml set launch mode "singleTask" for Activity2
<activity android:name="Activity2" android:launchMode="singleTask">
Put extras and start Activity2 from Activity1 (if Activity2 was started before, intent will be sended to existing instance).
Intent intent = new Intent(getContext(), Activity2.class);
intent.putExtra("CHECK_POINT", checkPointData);
startActivity(intent)
Override method onNewIntent() in Activity2
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
onNewCheckPoint();
}
private void onNewCheckPoint(){
Intent intent = getIntent();
Bundle extras = intent.getExtras();
//in this moment, you can process data, like you want.
}

Categories