I am adding some large serializable object (say data) to the Intent's putextra() method:
Intent intent = new Intent(currentScreen, newScreen.getClass());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle bundle = new Bundle();
intent.putExtra("DATA", data);
intent.putExtras(bundle);
currentScreen.startActivity(intent);
newScreen taking much time to start and display.
Please let me know how to overcome the issue.
Thanks
Android_IT
Don't just assume the large extra data is causing your app to be slow. Profile it with Traceview and make sure. If it does indeed turn out to be the problem, my only suggestion is to store it in a static member before starting the new activity and then retrieve it from there. This way it will not be copied around and serialized/deserialized.
If NewScreen take too much time you are probably doing some time consuming task in the onCreate method of NewScreen.See what is happening there by printing some log to see whether onCreate of newScreen fired immediately or not
Related
I'm starting an Intent for choosing an Image from Gallery. My target is to put an Extra (an int value) to onActivityResult, but I can figure out how to do this.
The way I call the image chooser:
Intent photoPickerIntent = new Intent();
photoPickerIntent.setType("image/jpg");
photoPickerIntent.setAction(Intent.ACTION_GET_CONTENT);
Intent photoResultIntent = Intent.createChooser(photoPickerIntent, "Bild auswählen");
fragment.startActivityForResult(photoResultIntent, HorseFragment.PICK_IMAGE, bundle);
What I tried (with no success):
instantiating a new Bundle and give it to startActivityForResult() as third parameter
using photoResultIntent.putExtra...
How can I put a value to my target activity and retrieve it in onActivityResult? Can anyone explain it to me?
Thank you a lot for your time and your help!
I am new to Android development and I have come across something new while using Intent as getIntent().getExtras().
Can anyone please explain me how can we write getIntent().getExtras(),cause till now what I know is we can call method by creating object of that particular class but here we are call method getExtras() by using getIntent()method.
getIntent().getExtras() is used to get values from intent that are stored in bundle.
Intent class is used to switch between activities. But sometimes we need to send data from one activity to another. So, at this particular moment we need to set some values to intent that can be transferred to destination activity. We can achieve this by the following code -
Bundle bundle = new Bundle();
bundle.putString("key1","someValue");
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
intent.putStringExtra("key","value");
intent.putExtras(bundle);
startActivity(intent);
Now, in the second activity we can get the value of "key" so we can use that in second activity. To do so, we use the getIntent().getIntent can store a Bundle. Let's see an example -
Intent intent=getIntent();
Bundle valueFromFirstActivity = intent.getExtras();
String valueOfKey = intent.getStringExtra("key");
String valueOfKey = bundle.getString("key1");
So this way, one can get values from activities. Bundle is a class that can hold values within itself and that instance of bundle can be given to intent using putExtras(). It is quite helpful in transferring the custom array list.
we can call method by creating object of that particular class
getIntent() is a method that returns an Intent object. When you call getIntent().getExtras(), first an Intent object is returned and then getExtras() method is invoked.
This way of calling is referred to as method chaining(Fluent Interfaces)
In my game I am trying to pass the score from the PlayGame activity to the Scoreboard activity using an Intent Extra.
On finishing the game, I go to the scoreboard in this way:
Intent intentScoreboard = new Intent(getApplicationContext(), Scoreboard.class);
intentScoreboard.putExtra("com.example.game.SCORE", score_counter);
startActivity(intentScoreboard);
and then in the Scoreboard class I retrieve it in the onResume() method like this:
Bundle b = getIntent().getExtras();
int score = b.getInt("com.example.game.SCORE");
This works fine the first time, but if I then play another game and on finishing return to the scoreboard, I still get the score from the first game.
What am I missing?
you are missing calling setIntent()
try this:
let your score activity do a finish() if you get back to start a new game
then it should be working
getIntent delivers the intent which started the activity. If the activity is resumed you get not the most recently received intent. See here for the solution: https://stackoverflow.com/a/6838082/1127492
Bundle is not much needed to receive getExtra() values.
In my code, i have used to receive like this,
int score = getIntent().getIntExtra("com.example.game.SCORE",defaultValue);
It should works for your problem. And also it won't gives you the already received values.
Hope it sounds good for you dude.
I have some values and want to pass with activity so that I can show in TextViews of Activity, I am unable to understand such a concept, so what should I do?
Simply I want to make constructor but unable to understand it that how will it be done, I am new to android programming, so needed help.
Start by reading the documentation at developer.android.com about intents and intent extras.
In android if you launched an activity there is a method called onCreate execute automatically.You can send values using Intent to the activity and can retrieve them in the Activity
The Activity (sub)class must have a default constructor without any parameters so that the system can instanciate it at run time.
To pass "parameters" to activities, you need to use the extra bundle of the intent.
Intent i = new Intent(this, MyActivity.class);
i.putExtra("com.sample.MyParameter", 666);
startActivity(i);
See Starting An Activity
If you want to pass one value then you can use intent but if you want to pass multiple values then using "Bundle" is best way.
Bundle bundel = new Bundle();
bundel.putStringArray("key1",strings);
bundel.putStringArray("key2",stringsofids);
bundel.putString("key3", str31);
bundel.putStringArray("key4",stringsbakup);
bundel.putString("key5", str1);
bundel.putString("key6", str4);
Assuming i want to open another activity from my current activity and i want to pass arguments such as in my case difficulty level how do I do it?
newGameButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(countryCityGameMenu.this,GameScreen.class);
startActivityForResult(i, GlobalDataStore.STATIC_INTEGER_VALUE);
}
});
is there a way to pass those arguments in the calling ?
can someone show an example explaining
what send activity should do
what the new born activity should do
As ben already mentioned you can add this data to the intent inside an extra bundle.
An extra bundle stores data as a key value pair of native java data types.
You can add data to the intent via the putExtra methods.
In the new Activity you can retrieve this data via the getExtra methods of the Intent. For example the getStringExtra method.
To get the intent that started the current activity just use the getIntent() method from activity.
You have to use extras like this:
i.putExtra(varName, value);
Not a big fan of this approach... but unfortunately it sends to be the only way to do it in android...
fine.. U can also use StartActivity(intent); Thats enough to pass