Android New Intent New Screen - android

I am still not completely sure of this opening a new screen with a new intent. I have two problems. 1 is getting it to work and the second is more theory.
Firstly I have two packages com.quiz.max and com.reason.max both have activities names accordingly eg Quiz and Reason respectively. Here is the on click code I am trying to execute at the moment in quiz to go to reason.
Intent intent = new Intent();
intent.setClassName("com.reason.max", "com.reason.max.Reason");
this.startActivityForResult(intent, requestCode);
Secondly I heard if I start this intent then everytime i click the button a new intent is created. Does this mean if the user goes to reason page and navigates back and clicks the button again they actually create a new intent instead of going back to the already active one. Thus dozens could be opened via this method. Therefore should I close each reason intent once navigated back or is this a redundant point?
Max

I think you want
Intent intent = new Intent(this, Reason.class);
startActivityForResult(intent, requestCode);
Secondly, you don't "start an intent". You use an intent to ask an Activity to start, in this case the Reason activity. And yes, the default behavior is to start a new instance of the activity each time it is requested.
You can alter this behavior with the launchMode.
Make sure you read and understand the Activity lifecycle. You don't need to worry about too many Activities in existence, Android will handle that for you, but you should properly save state and clean up connections in the appropriate lifecycle methods.

Related

FLAG_ACTIVITY_NEW_TASK when you create an exit button

I was reading this question because I want to create an exit button for my android app. I found the following answer
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
but I don't understand why he set the flag Intent.FLAG_ACTIVITY_NEW_TASK. What exactly this flag is gonna do? Can I omit it? Which is the best approach?
By the way I got confused when I read
the current task will simply be brought to the front of the screen
with the state it was last in
from the official documentation (here). I thought that FLAG_ACTIVITY_NEW_TASK will create a new back-stack and hence lose the state it was last in...
This flag is required when you start an activity from outside of an activity,like service or another activity outside the current app. It is actually used for making another instance of the activity as a new task.Remove the flag and it should work. if it does not, try using flag Single top.If you are planning to exit from the app the flag is not required.you can remove it.

Destroy an Intent?

When I create a Intent:
Intent in = new Intent(this, myclass.class);
this.startActivity(in);
I create a new Intent but the last Intent is not destroy
It's still in memory
How can I kill this last destroy?
So I want to create two Intents in two different classes:
in the first class I write this:
Intent in1 = new Intent(this, myclass2.class);
this.startActivity(in);
and in second class I write this:
Intent in2 = new Intent(this, myclass1.class);
this.startActivity(in2);
but when I Click in1 will create and the in2 will destroy, and when i Click on in2 the in2 will create and in1 will destroy.
a want to clean my phone memory.
You can prevent the user from going on to the previous Activity by overriding the back key functionality like this:
public void onBackPressed() {
//doing nothing on pressing Back key
return;
}
Though this approach is not encouraged.And as everybody knows, the Activity will be automatically killed when certain aspects come up like memory requirement. You cannot destroy one Activity at your free will.
Though for specific cases like a welcome splash screen maybe, you can do one of the following two things:
1) call finish() method on your current Activity as you move onto
your new Activity(generally done when using Thread).
2) use the following in your manifest:
<activity android:name=".WelcomeScreen" android:noHistory="true" ... />
This will tell the device to not keep this Activity on the Activity stack.
Activities remain in memory until Android decides it should be removed. Don't worry about it. It'll get destroyed when more memory is needed. It's effectively "unused" memory until your top activity uses it.
I'm not sure to understand your question...
You want to start a new activity and destroy the previous one?
If this is what you need, you can use:
startActivity(new Intent(this, myActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
The "CLEAR_TOP" flag tell "Hey! I want to start this activity and kill all the other ones!"
Since the Android runtime environment uses garbage collection (just like "traditional" Java), you won't need to worry about manually destroying objects in most cases. The memory taken up by the unused Intent object will be recycled if the system needs additional space for new objects.

Android - start multiple activities

is it possible to start multiple activities at once? I mean, from main create 3 activities in some order and just the last will be visible? Up to now, I was able to create only one activity.
Thanks
You might need something like this in order to launch deep into the app after the user has clicked a notification in order to display some newly added content, for example.
Intent i = new Intent(this, A.class);
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(i);
Intent j = new Intent(this, B.class);
j.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(j);
Intent k = new Intent(this, C.class);
startActivity(k);
In this way you can start activities A, B and C at the same time and suppress transitions to activities A and B. You get a single transition from your current activity to activity C. I strongly suggest that you log the Activity lifecycle method calls (onCreate etc.) to LogCat, for example. It helps a lot in understanding the order of events.
This can be a common thing to do in response to deep linking or other use cases where you, basically, need to synthetically rebuild the Task (and all the activities it should contain). Sometimes, just specifying parents in the manifest isn't enough.
Take a look at TaskStackBuilder. One common example:
TaskStackBuilder.create( context )
.addNextIntent( intentOnBottom )
// use this method if you want "intentOnTop" to have it's parent chain of activities added to the stack. Otherwise, more "addNextIntent" calls will do.
.addNextIntentWithParentStack( intentOnTop )
.startActivities();
Really old question but I thought I still answer it.
Use:
public void startActivities (Intent[] intents, Bundle options)
Try startActivity(new Intent(...); at the end of your onCreate-Method of the first Activity.
This will immediatly launch a new Activity and pause the first one.
With back-key you will get back to the last Activity

Synchronous system activities

I wanna run two system activities one after another in specific order.
now as we know, startActivity is an asynchronous operation, so i cant keep on a specific order.
so i thought maybe I should try to do it with dialogBox in the middle but also running a dialogBox is an asynchronous.
now as i said the activities which i try to run are system activities, so i cant even start them with startActivityForResult (or mybe i can, but i cant think how it will help me).
Any tricks how could i manage with this issue?
Some code:
first activity:
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Settings.ACTION_APPLICATION_SETTINGS);
startActivity(intent);
second activity:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(tmpPackageFile
.getAbsoluteFile()),
"application/vnd.android.package-archive");
startActivity(intent);
as you can see, i dont have any access to those activites, i can just run thire intents from an outside class/activity/service.
You should be able to use startActivityForResult.. The second parameter to that function is a unique id, which you can use to track which activity is ending.
In onActivityResult of the calling activity, check which activity just finished, then start the next one with another call to startActivityForResult (or, if you don't care what happens with the 2nd, just startActivity).
I may be missing the boat on this, it seems you should place your code to start the second activity in the handler that finishes the first activity, such as on a button press or when an item is selected from a ListView. More information on how the first Activity is terminated would help.

Android: How to kill the current activity if the new activity is the same with the current one?

I know it's kinda hard to understand the question, I'm new to Android, what I mean is that:
say I have an activity A that is currently active, then I put device sleep and wake up the device, activity A still active right now.
At this time a dialog will pop up and I press "Yes", a new activity A will be created. What I concerned is that, how do I kill the old A and then create the new A?
Right now when I click "Yes" the new A is created but it's not showing correctly.
I'm not sure what are trying to achieve, but this code snippet should do what you want.
finish();
Intent intent = new Intent(this, YourActivity.class);
startActivity(intent);
A different alternative is calling
recreate();
As the documentation says: This results in essentially the same flow as when the Activity is created due to a configuration change -- the current instance will go through its lifecycle to onDestroy() and a new instance then created after it."
Try this, hope this will help.
Intent intent = new Intent(this, yourclass.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();

Categories

Resources