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);
Related
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)
Is there a way to set parameters that they would receive as arguments in the constructor were they initialized as standard objects instead of by intents? I can't use parameters stored in the intent because sending an intent doesn't necessarily initialize the activity / service (it may exist already). Can I use the manifest file to set custom parameters?
Thanks.
Starting an Activity will always start activity, and if activity is already in stack, then also it would launch the activity, unless some intent filter havent been specified.
For Service, if you start service by startService(), onStartCommand() method will be invoked, which has an Intent as parameter, you can get values passed from this parameter.
sending an intent doesn't necessarily initialize the activity / service(it may exist already).
What if you keep using Intents but erase the history of the TargetActivity each time you call it.
Intent intent = new Intent(this,TargetActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
I found a possible solution here: Is it possible to have custom attributes in AndroidManifest.xml tags?
Doesn't help if I want to pass objects, but for simple types it's good enough.
In Android 2.3.3, how does one differentiate Intents that start a particular Activity. For example, if both Activity_A and Activity_B have intents that call startActivityForResult( intent, requestCode), how does Activity_C differentiate between which Activity has started it? Also, I know that one passes a requestCode to the starting Activity, but how does this Activity handle the requestCode? There is no method in Intent that says getRequestCode(). Is the only way to do this to place the requestCode in a Bundle in addition to the method startActivityForResult? Thanks!
Intent API:
http://developer.android.com/reference/android/content/Intent.html
One solution would be to pass along an extra piece of identifying data. For example:
intent.putExtra("activity", "com.whatever.MyActivity");
Then the receiving Activity can read it:
Bundle extras = getIntent().getExtras();
String activityName = extras.getString("activity");
It seems like there should be an easy method call to tell what the sending Intent was, but if so, I'm not aware of it.
I was wondering, how would I make a switch statement, that when that certain case was triggered, it would open a new screen with text. Would I use an intent? And if so, which one?
Thank you for your help in advance.
When you want to open a "new screen", you probably want to open a new activity. You would create a second Activity-derived class and use the following overload of the Intent constructor with startActivity:
Intent intent = new Intent(this, MySecondActivity.class);
startActivity(intent);
this will explicitly attempt to open a new Activity with the class name MySecondActivity
to pass a String of text from one Activity to another in this way, you can add it to the intent.
String someValue = "Some Value";
intent.putExtra("Some Key", someValue);
and in the code of your other Activity, you can get at this string via the Intent
getIntent().getStringExtra("Some Key");
Obviously you want to do null checks to make sure the key exists in the Intent, and you want to put a proper constant String somewhere instead of using a literal String for a key, but this is the basic gist.
Kriem... As Rich said you can launch a new activity using intents and push data to the new activity using extras. You can push data back to your main activity using startActivityForResult instead of startActivity. You can return to your main screen by calling finish() in the new screen. Finally, you can put the new screen event handlers into the NewScreen.java file.
The overall effect is a near full separation from dependency between the two activities, so that you might be able to easily re-use the NewScreen.java class in another project. I have some code here.
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