build a "fake" stake using startActivity() - java

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

Related

How to go back to previous activity after clearing task?

Sorry if title is not clear, here's an in-depth explanation:
I'm in the Settings activity of my app and I have an option to change the app's theme. Obviously I want the entire theme of the app to change immediately when I change this option. So I'm using OnSharedPreferenceChangeListener in this way:
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("theme_preference")) {
Intent refresh = new Intent(getApplicationContext(), SettingsActivity.class);
refresh.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); //clears previous activities
startActivity(refresh);
}
}
As you can see I added flags that would clear the previous activities so that they'll be recreated too to have their themes updated.
The Settings activity recreates itself just fine, but then when I press the Up or Back button to go back to the previous activity the app crashes. Why is it crashing?
This is how you start a stack of activities:
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
stackBuilder.addNextIntentWithParentStack(intent);
Intent nextIntent = ...;
stackBuilder.addNextIntent(nextIntent);
stackBuilder.startActivities();

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.

Starting an Activity from a Notification and passing data onBackPressed

I have 3 Activities on the app:
MainMenuActivity -> ExecuteTrainingActivity -> ExecuteExerciseActivity.
From MainMenuActivity to ExecuteTrainingActivity, I'm passing an idExecution and an idExercise for ExecuteTrainingActivity query and load the initial data.
ExecuteTrainingActivity onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
//...
initialize();
setupRecyclerView(exercises);
}
private void initialize() {
Bundle extras = getIntent().getExtras();
if (extras != null) {
if (extras.containsKey("id_execution")) {
idExecution = extras.getLong("id_execution");
idExercise = extras.getLong("id_exercise");
execution = queryExecution(idExecution);
} else {
insertExecution();
}
}
}
In the 3rd activity, ExecuteExerciseActivity, I have a TimerFragment that, and when TimerCountdown reaches 0, it opens a Notification popup, that when clicked open an fresh ExecuteExerciseActivity.
On this TimerFragment, I'm passing as Extras the same ids, so I can get them in the new fresh ExecuteExerciseActivity:
public class TimerFragment extends Fragment {
//...
private void showNotification(){
Intent intent = new Intent(getActivity(), ExecuteExerciseActivity.class);
intent.putExtra("id_execution", idExecution);
intent.putExtra("id_exercise", idExercise);
intent.putExtra("position", position);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getActivity());
stackBuilder.addNextIntentWithParentStack(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
/*=== CHECK IF NOTIFICATION CHANNEL IS ACTIVE ===*/
boolean ok = isNotificationChannelEnabled(getActivity(), Constants.CHANNEL_ID);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(requireNonNull(getActivity()), Constants.CHANNEL_ID)
.setSmallIcon(R.drawable.d77)
.setContentTitle("Teste Notificação")
.setContentText("Ababa")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getActivity());
notificationManager.notify(0, mBuilder.build());
}
From this new fresh ExecuteExerciseActivity, I want to make the system maintain the same navigation flow of Activities, but when I backpress from the new ExecuteExerciseActivity to ExecuteTrainingActivity, i can't pass the Id's for the ExecuteTrainingActivity query and load.
Is there a way to pass arguments onBackPress?
Is the best approach override onBackPress creating a new intent and a starting a new Activity?
**My manifest is using parentActivityName correctly.
Save your query and id into a SharedPreferences in onDestroy of your ExecuteExerciseActivity then pull out the query and id again in the old ExecuteTrainingActivity. onBackPressed triggers onDestroy event of an activity's life cycle. Then in the onResume of ExecuteTrainingActivity pull this data back out.
I think you can achieve this by overriding onOptionsItemSelected() method of ExecuteExerciseActivity. Try this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Intent intent = new Intent(this, ExecuteExerciseActivity.class);
//Add the extras to the intent
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
In Android, if your Activity starts another Activity and want to pass data to that Activity, you would add those data as Intent extras.
This Activity A to Activity B type of sending data is something you're using already.
But for Activity B back to Activity A, there's actually a built-in solution for this, which is through startActivityForResult(Intent, REQUEST_CODE) instead of startActivity(Intent).
Now in Activity B, you simply have to code:
#Override
public void onBackPressed()
{
Intent resultIntent = getIntent();
resultIntent.putExtra(EXTRA_NAME, extra_value);
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
Basically, in Activity B, you're setting the data you want to send back to the Activity A that started Activity B, since there's a connection between these two Activities.
Next, in your Activity A, just override the onActivityResult() method.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// get data through data.getIntExtra() and etc
}
}
}
The request code needs to match the REQUEST_CODE you used for Activity B, so Activity A knows which Activity to respond to. The result code is just a quick way for you to categorize the type of result you're getting back from Activity B, since there might be more than one type of result being returned from Activity B.
This solution of passing data is better than creating a new Intent and starting a new Activity because you don't need to start a new Activity. Your Activity A already exists, so there's no reason to rebuild the entire layout, reload all the data, and have a new Activity existing on the Activity stack.
Since your only intention is to pass data from Activity B back to the Activity A that started it, use startActivityForResult() and onActivityResult() to handle this type of data sharing.

How to close all running activities from another one?

I've got some activities which I execute one after another without closing (1->2->3->4). I want to close 1,2,3 activities from 4 and execute a new one; the code:
Intent intent = new Intent(UserpickActivity.this, CommunicationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
UserpickActivity - 4th activity, CommunicationActivity - the next activity. But after this code all previous activities still worked. How can I close all running activities and execute some new activity?
Try this:
When you want to launch CommunicationActivity from Activity4, do this instead:
Intent intent = new Intent(this, Activity1.class); // Your root activity
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Will remove all other activities from the task
intent.putExtra("foo", true); // This is to tell Activity1 to launch Activity4
startActivity(intent);
In Activity1.onCreate() do the following:
super.onCreate();
if (getIntent().hasExtra("foo")) {
// We should now launch CommunicationActivity and finish ourselves
Intent intent = new Intent(this, CommunicationActivity.class);
startActivity(intent);
finish();
return; // Don't continue the rest of onCreate()
}
// The rest of your onCreate() goes here
You can only use CLEAR_TOP to clear down to an existing activity in the activity stack. That's why your use of CLEAR_TOP isn't working (because CommunicationActivity doesn't exist in the activity stack).
This method clears all activities and launches the root activity again. When the root activity's onCreate() is called, it checks the Intent for the extra "foo", which it uses as a signal that it needs to launch CommunicationActivity and finish itself. This should do what you want.
Call activity.finish(); after you switch to other activity. This way no activity will remain on stack.
Intent intent = new Intent(UserpickActivity.this, CommunicationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
//YOUR_ACTIVITY.finish();
Uncomment the above line if you want this activity to be removed from activity stack.
// This will clear all activity and start new task.
Intent intent = new Intent(UserpickActivity.this, CommunicationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Use android:nohistory=true in manifest for Activities.
<activity
android:name=".A"
android:label="#string/app_name"
android:noHistory="true" >

How to restart activity using intent?

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

Categories