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.
Related
I am developing an Android Kotlin application which uses two different activities. In each one I have a button that allow me to move to the other one, to say, in Activity 1 I have a button that calls the follwing:
val intentActivity2 = Intent(this, Activity2::class.java)
startActivity(intentActivity2)
This launches correctly Activity2, which similarly inside it I have another button that calls the first activity to return to it:
val intentActivity1 = Intent(this, Activity1::class.java)
startActivity(intentActivity1)
The problem I have is that I want to have both activities running simultaneously (not needed to be shown at screen at the same time), and the issue right now is that every time I call the "startActivity(intent)" a new activity is created, loosing what I had in the previous one, so when I return to Activity1 everything is reset and the same when I go once again to Activity2. Both activities work fine and do their work, the problem is that I can't freely conmute between them.
Is there a way to have both activities at the same time or to not start a new activity everytime I want to change to the other one?
Thank you,
As someone just said you need ViewModel to retrieve your data after deleting/creating new activities. Maybe you can use fragments to do your things. Fragments are attached to activities and it is easier to use them instead of ViewModel.
I am navigating from Activity A to Activity B without finishing Activity A (because i want to go back on it and have some variables values).
In Activity B I launch camera and save captured image but the problem is, after capture camera the Activity A is re-create and resumed, causing re initialization of my variables.
How to stop it?
Note: The problem occurs only in Nougat Version.
The thing you want to know here is about the complete activity life cycle. Basically to summerize them they are
OnCreate, - called when activity is created
OnStart, - called when activity starts
OnREsume, - when activity gets back
OnPause, - when activity is overlapped
OnStop, - when activity is closes
and onDestroy - when finish() is called.
Though you have not mentioned how you started an activity and got back to the same activity, the correct way to get back to previous activity is by calling
finish()
on camera activity.
I guess you have got back to activity A by using
Intent i= new Intent(this, ActvityA.class)
startActivity(i)
which is the the correct way to do.
Just call the finish() when you want to get back to Activity A from B (Here B is in top of A).
Additional
If you want to pass data from activity B to A, just put something called as Intent Extra or Bundle
Activity A is create again and resume, so my variables are initialize again. How to stop it.
Yes that is normal. First rule with Android programming is that your activity can be killed at any time. So to not loose your variables you have to save them at the right moment.
You would do that to override onSaveInstanceState() putting your variables in the bundle.
Then you can retrieve them in onCreate() from the function parameter.
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.
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.
i have four activities.
like (1) (2) (3) (4).
(1) is first activity or main activity.
i have a bottom bar for these activities.
if i click on (2) i want to open second activity.
after this if i click on (3) i want to open third activity.
and if i click on (4) i want to open fourth activity.
after this if i click on (1) i want to display first activity.
without finishing any other activity or again open (1) activity.
a image for easy understanding is attached..
please open this image in new tab to view clearly.
i want to do this without using tabhost.
can it done by using activity group.
suggest any example or tutorial for this.
thanks Rock Brown
This use case is already built into the platform:
In your AndroidManifest.xml there is an activity element for each activity. In the activity element for 1 set the launchMode:
android:launchMode="singleTask"
This causes the platform to only ever launch one instance of Activity 1 in the app's task (which is a stack of activities.)
When you start activity 1, 2, 3, or 4 set the Intent's flags to include FLAG_ACTIVITY_REORDER_TO_FRONT:
intent.setFlags(intent.getFlags() | FLAG_ACTIVITY_REORDER_TO_FRONT);
This causes the platform to bring any existing instances of 1, 2, 3, or 4 to the top of the activity stack, rather than creating a new activity and placing it on top of the stack.
An activity can be closed at any moment by the os if a memory pressure append and your activity is in the background.
I agree with Aashish that you probably want to look at the TabHost tutorial.
But, yes, you can do it without TabHost.
Activity (1) will get launched first, assuming it is the launch activity as defined in your manifest. You can switch to one of the other activities at any time using Context.startActivity(Intent).
You can even do this before (1) is displayed based on state preserved in a bundle or saved to user prefernces (i.e. so it restarts on the same activity that was last used). In a case like this you don't want to the current activity to be on the backstack so you call Activity.finish() after launching the next activity, so now you have.
public void showNextActivity() {
Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
finish(); // so ThisActivity isn't in the backstack.
}
You should probably have some code that is shared between your activities to manage the widgets that are used to change between activities (e.g. the Button's) and to handle the switching. Note that, even if each activity has its own resources, you can use the same r esource ID's on each - so the Button that selects activity 2 has ID 'act2' on each activity. This makes sharing code easier.