Sending ApplicationInfo from one activity to another - android

This is how I send it
Intent i = new Intent(v.getContext(), Permissions.class);
i.putExtra("AppSelected",installedApps.get((int) id));
startActivity(i);
THis is how I retrieve it:
Bundle extras = getIntent().getExtras();
ApplicationInfo a = extras.get[What do I put here?]("AppSelected");
I cannot seem to figure out get this to work. Any thoughts?

Call getParcelable() on the Bundle to retrieve your ApplicationInfo (which implements the Parcelable interface). You will need to cast the result to ApplicationInfo.

Assuming installedApps.get... returns an integer, you should change your code
Bundle extras = getIntent().getExtras();
for
int appId = getIntent().getIntExtra("AppSelected", 0);

to retrieve any extra just use the key that you use to save value plus a default in some case
example
Bundle extras = getIntent().getExtras();
int example = extras.getIntExtra("key", int defaultValue) ;//to retrieve an Integer
String example = extras.getStringExtra("key"); //to retrieve a String
.....son so forth
in your case
Bundle extras = getIntent().getExtras();
ApplicationInfo a = extras.getIntExtra("AppSelected", 0);

Related

Get String Value Dynamically

I am passing a value from one activity to another through putExtra() and getExtra() and getting value like:
Passing from
Activity 1:
intent = new Intent(this, blankScrollActivity.class);
intent.putExtra("alphabets", "a");
Recieving at
Activity 2: blankScrollActivity.class
Bundle extras = getIntent().getExtras();
String varName = extras.getString("alphabets");
TextView textView = (TextView) findViewByid(R.id.blankTextView);
String Resource MyString.xml:
<string name="a">My Example 1</string>
<string name="b">My Example 2</string>
I want to get the value of string dynamically to assign textView.
Bundle extras = getIntent().getExtras();
String varName = extras.getString("alphabets");
int resId = getResources().getIdentifier(varName , "string", getPackageName());
String data = getResources().getString(resId);
This way, you will get the String resource that you want based on the String value sent from the first Activity.

How to use the string from editText as display textview in another activity?

I want to use the string which is typed in edittext as textview in a new activity.
sub6 = (EditText)findViewById(R.id.editText16);
txt6 = (TextView)findViewById(R.id.textView25);
subject = (String) sub6.getText().toString();
txt6.setText(getResources().getString(R.id.editText16));
I have used this code. But is of no use. Can you please help me?
You can send the string using Bundle
Intent i=new Intent(A.this, B.class);
Bundle b = new Bundle();
b.putString("name", sub6.getText().toString());
i.putExtras(b);
startActivity(i);
and in the receiving activity:
Bundle extras = getIntent().getExtras();
String text = extras.getString("name");
Here's how I do it:
EditText sub6 = (EditText) findViewById(R.id.edittext16);
TextView txt6 = (TextView) findViewById(R.id.textview25);
String subject = sub6.getText().toString();
txt6.setText(subject);
You do a lot of unnecessary casts. Like your third line casts a String to a String. Which is useless. Also, declare the object types (like String, EditText, etc.) before declaring them.
In MainActivity,send the string using intent
sub6 = (EditText)findViewById(R.id.editText16);
txt6 = (TextView)findViewById(R.id.textView25);
subject = sub6.getText().toString();
Intent in=new Intent(FirstActivity.this,SecondActivity.class);
in.putExtra("key",subject);
startActivity(in);
In the second Activity recieve intent using getIntent() andthe string using key getStringExtra()
Intent in=getIntent();
String s=in.getStringExtra("key");
txt6.setText(s);

Pass ArrayList of integer array via bundle

I'm looking for a way to pass, via bundle, an
ArrayList<Integer[]>
object to a Fragment (after activity is already created, so i can't use intent). By looking to android api, no method seems to do what i'm looking for. How can i do?
Sending activity:
final Intent intent = new Intent(this, SecondActivity.class);
Bundle extraBundle = new Bundle();
extraBundle.putIntegerArrayList("arraylist", [put array list here]);
intent.putExtras(extraBundle);
intent.setComponent(new ComponentName("com.myapp", "com.myapp.SecondActivity"));
startActivity(intent);
Receiving activity's onCreate():
final Intent intent = getIntent();
final Bundle extraBundle = intent.getExtras();
ArrayList<Integer> myIntegerArrayList = extraBundle.getIntegerArrayList("arraylist");
You can change "arraylist" to what you want in the setter and getter method calls, they just need to be the same.
I used Gson, convert to JSONArray and send via Bundle. But it may affects performance.
In First Activity
Intent activity = new Intent(MyActivity.this,NextActivity.class);
activity.putExtra("myArrayList", new Gson().toJson(myArrayList);
startActivity(activity);
In other activity..
Sting myArrayList;
Bundle extras = getIntent().getExtras();
if (extras != null) {
myArrayList= extras.getString("myArrayList");
Type listOfInteger = new TypeToken<List<Integer>>(){}.getType();
String s = new Gson().toJson(myArrayList, listOfInteger);
List<Integer> myList = new Gson().fromJson(s, listOfInteger);
}
int[] intarray = new int[] {4,5,6,7,8};
Bundle bundle = new Bundle();
bundle.putIntArray("integerarray", intarray);
Or an ArrayList with int arrays?
For that i think you should use Parcable.
Or you could try to send the individual intArrays via the bundle and put them together in the receiving Activity.
Like this:
int[] intarray1 = new int[] {4,5,6,7,8};
int[] intarray2 = new int[] {4,5,6,7,8};
int[] intarray3 = new int[] {4,5,6,7,8};
Bundle bundle = new Bundle();
bundle.putIntArray("INT_ARRAY1", intarray1);
bundle.putIntArray("INT_ARRAY2", intarray2);
bundle.putIntArray("INT_ARRAY3", intarray3);
Intent intent = new Intent(this,NewActivity.class)
intent.putExtras(bundle);
startActivity(intent)
Then in NewActivity.java you should create an array and get the bundle and fill the array with the received arrays.
you can pass the Array List of integers by using the method putIntegerArrayList
Here is the code snippet
ArrayList<Integer> values=new ArrayList<>();
values.add(1);
values.add(60);
values.add(75);
values.add(120);
Bundle extras = new Bundle();
extras.putIntegerArrayList(EXTRA_KEY,values);

null value in getIntent.getExtras.getString()

This is my code in my first activity:
Intent i = new Intent(this, OtherScreen.class);
i.putExtra("id1", "first");
i.putExtra("id2", "second");
startActivity(i);
where first,second are the values I want to be passed.
and on my other class i have this:
Intent i = getIntent();
Bundle extras = i.getExtras();
String result = extras.getString("id1");
System.out.println("yeah"+result);
but after i run it, my result is null.Can you help me?
If I write my getString in that ways, I am getting syntax errors.
String result = extras.getString(id1); //id1 cannot be resolved to a variable
String result = extras.getString("id1","default value"); // remove argument
Intent i = new Intent(yourcurrentactivity.this, OtherScreen.class);
i.putExtra("id1", "first");
i.putExtra("id2", "second");
startActivity(i);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String result = extras.getString("id1");
System.out.println("yeah"+result);
}
Alternatively for more accuracy, you can use
`if(getIntent.hasExtras("id11")'
before actually getting extras from the bundle and do some action if its null
try:
getIntent().getStringExtra("id11");
Of course its best to FIRST check if it even has the extra before you even go for it.
Ref:
http://developer.android.com/reference/android/content/Intent.html#getStringExtra(java.lang.String)
I had the same problem in my project.
intent.putExtra("key",editText.getText())
I have changed editText.getText() to String text.
String text = editText.getText();
intent.putExtra("key",text);
And that has solved my problem. Seems like it is an API problem.
You may try :String result = extras.get("id1");
I was also having trouble with null values. #YuDroid's suggestion to use if (getIntent.hasExtras("id11") is a good idea. Another safety check that I found when reading the Bundle documentation, though, is
getString(String key, String defaultValue)
That way if the string does not exist you can provide it with a default value rather than it being returned null.
So here is #AprilSmith's answer modified with the default String value.
Intent i = new Intent(yourcurrentactivity.this, OtherScreen.class);
i.putExtra("id1", "first");
i.putExtra("id2", "second");
startActivity(i);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String result = extras.getString("id1", "myDefaultString"); // This line modified
System.out.println("yeah"+result);
}
If anybody still having this issue..here is the solution
the datatype you are passing in first activity should be same in receiving end.
'Intent myIntent = new Intent(D1Activity.this, D2Activity.class);'
'myIntent.putExtra("dval", dval);'
' D1Activity.this.startActivity(myIntent);'
in D2Activity
Bundle extras = getIntent().getExtras();
if (extras != null) {;
edval = extras.getInt ( "dval" );
}
here extras.getString() will not work as we passed an Int not a string

Passing Integer Between Activities and Intents in Android Is Always Resulting in Zero / Null

I'm attempting to pass two integers from my Main Page activity (a latitude and longitude) to a second activity that contains an instance of Google Maps that will place a marker at the lat and long provided. My conundrum is that when I retrieve the bundle in the Map_Page activity the integers I passed are always 0, which is the default when they are Null. Does anyone see anything glaringly wrong?
I have the following stored in a button click OnClick method.
Bundle dataBundle = new Bundle();
dataBundle.putInt("LatValue", 39485000);
dataBundle.putInt("LongValue", -80142777);
Intent myIntent = new Intent();
myIntent.setClassName("com.name.tlc", "com.name.tlc.map_page");
myIntent.putExtras(dataBundle);
startActivity(myIntent);
Then in my map_page activity I have the following in onCreate to pick up the data.
Bundle extras = getIntent().getExtras();
System.out.println("Get Intent done");
if(extras !=null)
{
System.out.println("Let's get the values");
int latValue = extras.getInt("latValue");
int longValue = extras.getInt("longValue");
System.out.println("latValue = " + latValue + " longValue = " + longValue);
}
Geeklat,
You don't need to use Bundle in this case.
Do your puts like this...
Intent myIntent = new Intent();
myIntent.setClassName("com.name.tlc", "com.name.tlc.map_page");
myIntent.putExtra("LatValue", (int)39485000);
myIntent.putExtra("LongValue", (int)-80142777);
startActivity(myIntent);
Then you can retrieve them with...
Bundle extras = getIntent().getExtras();
int latValue = extras.getInt("LatValue");
int longValue = extras.getInt("LongValue");
System.out.println("Let's get the values");
int latValue = extras.getInt("latValue");
int longValue = extras.getInt("longValue");
Not the same as
myIntent.putExtra("LatValue", (int)39485000);
myIntent.putExtra("LongValue", (int)-80142777);
Also it might be because you do not keep the name of the Int exactly the same throughout your code. Java and the Android SDK are Case-sensitive

Categories

Resources