Learning how to make android apps and I did this tut. Summary of the tut is here:
http://sketchytech.blogspot.com/2012/10/android-simple-user-interface-activity.html
I'm trying to figure out how intents work. In the tut you create an Intent called intent, and in DisplayMessageActivity.java it creates an Intent called intent by calling "getIntent()".
Does the "getIntent()" function (or method (I'm most familiar with C)) just return the most recently created intent? Can there only be one intent at a time?
Thnks in advance for any responses!
All Activities are started by either the startActivity(Intent) or the startActivityForResult(Intent, int) methods. The intent tells the Activity everything it needs to know to display the correct information at launch. getIntent(), when called in an Activity, gives you a reference to the Intent which was used to launch this Activity.
getIntent() method gets the intent that called this activity.there can be more than one intent but you have only one intent visible at one time (since only one activity is visible at one time)
An Activity is typically created through an Intent. Assuming you are in your first activity:
Intent intent = new Intent(MyFancyActivity.class, Intent.ACTION_VIEW);
startActivity(intent);
This launches a new MyFancyActivity instance. From MyFancyActivity, you can retrieve the intent which lead to that instance creation. That is, the getIntent() method:
// this is the intent created in your first activity
Intent i = getIntent();
There are two primary forms of intents you will use.
Explicit Intents have specified a component (via
setComponent(ComponentName) or setClass(Context, Class)), which
provides the exact class to be run. Often these will not include any
other information, simply being a way for an application to launch
various internal activities it has as the user interacts with the
application. Implicit Intents have not specified a component; instead,
they must include enough information for the system to determine which
of the available components is best to run for that intent.
An Intent is a data class which holds information for an Activity that is about to be started. An Activity is a manager or controller for the view which is currently displayed on the screen.
Activities in the system are managed as an activity stack. When a new
activity is started, it is placed on the top of the stack and becomes
the running activity -- the previous activity always remains below it
in the stack, and will not come to the foreground again until the new
activity exits.
Related
A white screen blinks for few milliseconds when I simply go from login activity to Main activity with following clear back stack code.
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
overridePendingTransition(0, 0);
If I just make the startActivity without Flags (or say without clear backstack)
overridePendingTransition(0, 0);
It doesn't blink with white screen.
But I have to clear back stack and start a new activity with NO transition animation. So it doesn't blink/appear in white screen for few milliseconds.
Looking forward to get a perfect answer soon. Cheers !
Try setting noHistory="true" for login activity in the manifest and start HomeActivity without any flags. This should solve your problem.
Don't use Intent.FLAG_ACTIVITY_NEW_TASK. If you want to clear the backstack (and assuming your login Activity is the only one in the stack), just finish the current Activity:
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(intent);
overridePendingTransition(0, 0);
finish();
Intent.FLAG_ACTIVITY_NEW_TASK is likely the cause of the flash since it requires you to create a whole new "task" (which is a collection of Activities with a given history) rather than just reusing the current one. It's almost like navigating to another app at that point.
As I have mentioned in the question that startActivity without using flags makes no blink and showing white screen.
So I choose that.
Now the problem is to clear the stack of 1st Login activity when I am into Main Activity.
I resolved it by setting the context of Login activity into Application class using setter and getter method.
And Whenever You reached to Main Activity. It will check whether you have the context in Application class by getter method.
If it's a yes and is a login activity then make the context.finish which will clear the back-stack by removing the login activity from Main Activity. And set null value to setter in Application class.
Feel free to share any concern regarding this. Thank you.
This may be a noob question, but I've been searching for some explanation about it and wasn't able to find.
Well I have the A.class which is initiating an activity (A).
The user click in a button and we go to B.class, which also initiates a layout and I'm sending a putExtra("key",value) to the activity (B).
I receive it and works perfect!
Then I want to send a putExtra("key",value) back again to activity (A), but the user clicked in another button I started the C.class to do a background task of what he needs.
When the user goes back to activity (A), the getIntent().getExtras() is empty.
So my question is, changing classes (threads) or activities can mess your bundle?
Is there a way to prevent it?
I think you're very confused. The intent returned by getIntent is the Intent that started your activity. It will never change. If you have an Activity A that wants to start Activity B and get a result, Activity A must call startActivityForResult to start B, and B must set a new intent to be returned, call setResult, and then finish. THen onActivityResult in Activity A will be called, and passed the result set by B. The getIntent() will not return any results.
I have been trying to figure out why my Intent would not transfer string data from one activity to another activity? I seems I had set launchMode = singleTask in the manifest folder and when I changed launchMode to standard the Intent code worked as expected.
The MainActivity is the first activity in the stack I am guessing that I made the setting a number of months ago to try and prevent the user from using the back button to navigate back to the password log in page. (MainActivity)
I kind of get the Back Stack idea but WHY would this setting inhibit the intent from transferring data. my test for transfer was a System.out.println statement?
Suppose you have activities A and B. A is the one with android:launchMode="singleTask". A starts B. B then starts A, causing the existing instance of A to return to the foreground.
In that case, A is called with onNewIntent(), and that Intent will have the extras from B.
onCreate() is only called when an activity is created.
In my app there are three activities.
A can open B
B can open C, or return to A, or return to C (if opened from C)
C can open B, or return to A
I have implemented this using various RESULT_CODEs and onActivityResults. However, the stack becomes too large after prolonged use of the app!
I need to clear activities from the stack. Looking at my app's structure, the best clearing method revolves around clearing whatever is the second activity on the stack (assuming A is always the first/bottom activity on the stack). I made a quick painting to make it easier to understand:
Once B is added onto ABC, the previous B (second activity in the stack) is removed. Once C is added onto ACB, the previous C (second activity in the stack) is removed.
I can think of two methods that do what I describe:
clear whatever is the second activity in the stack
remove any activities in the stack that are the same as the one that I am creating (C creates B, so the other B needs to be cleared)
However, I have not found a way to implement this in code. I have tried using Intent.FLAG_ACTIVITY_CLEAR_TOP, but that clears everything in the stack except for A, before adding the new activity at the top.
So my question: How can I implement method 1 or method 2? (Does not matter which one)
Here is an example of me clearing all in the stack but A:
Intent newActivity = new Intent(this, B.class);
newActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(newActivity, MY_REQUEST_CODE);
I am also open to alternative methods. Maybe using FLAG_ACTIVITY_NO_HISTORY somehow?
So it seems like you would like to keep one instance of B activity and C activity. This can be done with adding an XML attribute in your Manifest.xml
<activity android:name="B" android:launchMode="singleTask" </activity>
"singleTask" - The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.
Refer to here for more information.
I have occurred the same problem , to kill some specific activity .. my solution was to implement StackActivityManager used for managing activity :
Each activity was saved on static list which contains all activity of my application
The saving of activity was done the mother class base activity
Each activity have a static and unique tag used later to indicate which activity will be remove from the stack of activity
The launch mode is singleTask to avoid duplicate activity in the stack of activity.
try to do method:
Intent intent = new Intent(Activity1.this, Activity2.class);
startActivity(intent);
//The finish(); method, destroy the current activity.
finish();
after call the next activity, then the current activity destroys.
I hope that it works. :)
remove this line
newActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Try adding in your code-
Intent newActivity = new Intent(this, B.class);
newActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
newActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivityForResult(newActivity, MY_REQUEST_CODE);
Note: What Cory Charlton wrote in his comment about Fragments is probably the best solution for anyone having similar problems. I recommend trying that route instead of my own solution. The only reason I went with my own solution is because I needed a quick solution, and had not the time to learn how to implement Fragments before a deadline.
I ended up solving the problem by having the A activity always start the C activity, instead of the B activity. The B activity no longer creates a C activity, it instead finishes in order to go back to C.
This way the stack never gets bigger than 3.
And to provide some code, I had to add this before finishing in the B activity, since it now no longer is passing an object, but is returning it:
setResult(RESULT_OK, getIntent().putExtra(KEY_MY_OBJECT, myObject));
I have realized one application with Android which contain two parts (activities)
1- Main activity receive GPs, calculate X,Y pixels on a Map
2- Showing/scrolling map after loading it from SD card.
The exchange between both activities is made every 20s by Intent and extras (X, Y plots on the Map)
All that is working properly.
The problem is each time i send intent I create a new Map and after many exchange the application crashes.
Is it possible to transfert data to one activity without creating new map? or other solution to modify OnCreate parameters of the second activity
Thank for help
It's crashing precisely because onCreate is run each time you switch activities. It's starting a new activity each time, so you have multiple instances of each activity and it runs out of memory.
To stop this happening you should set a flag on the intent like:
int iflags = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT;
Intent i = new Intent("com.you.yourapp.yourotheractivity");
i.setFlags(iflags);
// Apply your extras
startActivity(i);
This flag will cause it to reuse the other activity if it is in the background, so onCreate() is only run the first time, after this only onResume() runs
Add android:launchMode into your main activity in AndroidManifest.xml. Use either singleTask or singleInstance depending of your requirements.
<activity android:name="com.app.activity" android:launchMode="singleTask" ...>
Quotes from http://developer.android.com/guide/topics/manifest/activity-element.html#lmode:
singleTask
The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.
singleInstance
Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task.