For eg: I am using one Activity for Different UI pages with different he. So I want to check if an activity with a particular parameter in the intent exists. If it does just bring it to the top or else create another instance of an activity.
I have tried adding the flag FLAG_ACTIVITY_CLEAR_TOP to the intent but that does not solve my purpose.Here is some code: I want to create an activity only if there is a new title.
Intent intent = new Intent(activity, MyActivity.class);
intent.putExtra(DISPLAY_NAME, title);
activity.startActivity(intent);
Related
Intent intent = new Intent(MainActivity.this, Payment.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Opening coupon activity to apply coupon:
Intent intent = new Intent(Payment.this, Coupon.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
From coupon activity opening payment activity with updated data
Intent intent = new Intent(Coupon.this, Payment.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
The problem is, when I click on the back button on payment activity after applying the coupon, it opens the previously opened payment activity again (not the main activity).
onBackPressed() I don't want to do it static (like Intent intent = new Intent(Payment.this, MainActivity.class); and also don't want to use finish(); in payment activity.
Please help.
You can use FLAG_ACTIVITY_NO_HISTORY when launching the Payment activity.
See: https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_NO_HISTORY
This is a Simple Use case of startActivityForResult don't make it complicated with Intent Flags and launch modes.
Whenever you Open your Activity Coupon just use startActivityForResult and upon applying coupon send the result back to previous Activity (success/failure) or whatever result you want . Then you can change the UI as per the result you got in onActivityResult() .
You can follow How to manage startActivityForResult on Android or the latest way from the doc Getting a result from an activity .
As I create navigation activity I always use fragments.
But now I want to use activity instead of that.
Is it possible to do so?
Sure you can,
just start the new activity with an Intent :
Intent i = new Intent(getApplicationContext(), SecondScreen.class);
StartActivity(i);
I have a activity which i declare as launchMode="singleInstance in Manifest file. and using blow code to starting this activity:
Intent intent = new Intent(this, LockScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
To close this activity i am doing like: finishAffinity();
But some how when i finish this activity i can see another open activity instance of this. Can someone tell me how i can finish all OR only create a single one when i need to start this activity from different places inside my application?
I'm working on an android application and in the application I have a couple buttons that let user to pass to another activity. Now the way I'm doing the transitions between this Intents is like below:
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
With the above content I start an activity and from that activity I'm getting back to the MainActivity using this code:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
But i suspect this cause memory to be over used because I'm not actually getting back to the Main Activity that created when application started, I'm just creating another instance of MainActivity I guess. Is this really as i thought and if it is how can I get back to the activity that created in the beginning or if I can't do such thing how can I make app to let the previous activity go?
Passing an intent to startActivity() will create a new instance of the activity and add it to the front of the stack. So:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
is basically asking to create a new instance. If you want to go back to the activity just before the current one, call either:
finish();
Or,
super.onBackPressed();
In your solution you just have to press back button and you'll be back in first activity.
If you want to close it and after open new instance like you are doing in second activity just add
finish();
at the end of
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
You just need to call finish(); method
Intent intent = new Intent(this, DestinationActivity.class);
startActivity(intent);
finish();
I have an Activity that used to launch another one according to a button pressed, the new one will be either a whole new one or as like the currently running one, So, when launching a new one I'm getting the creation animation as the following:
but when launching the same Activity I got one of two behaviors:
1- when launching it vie recreate() the activity just blinks to change the statistics.(which is pretty normal to have)
2- when dealing with it as if it's another activity by using intent() with CLEAR_TOP flag I got the following :
ways I already used:
Intent intent;
// 1 (dealing with it as a whole new one by passing the Activity Class)
intent = new Intent(v.getContext(),
MyActivity.class);
intent.putExtra("EXIT", false);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
// 2 (just getting the current one directly)
intent = getIntent();
finish();
startActivity(intent);
result for the two ways :
So, how to open the same Opened Activity with the animation of opening a new one?(i.e. like in the first pic)
found the solution by using CLEAR_TASK intent flag which according to that link will cause any existing task that would be associated with the activity to be cleared before the activity is started.
So, it will be the following way:
Intent intent = new Intent(v.getContext(),
MyActivity.class);
intent.putExtra("EXIT", false);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); // Here We Are
startActivity(intent);
finish();