When I'm trying to pass data to another activity it for some reason gets lost. My sending activity looke like:
protected OnItemClickListener onArtistItemClick = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View arg1, int cursor, long arg3) {
Intent artistCardIntent = new Intent(getBaseContext(), ArtistCardActivity.class);
Artist artist = (Artist) adapter.getItemAtPosition(cursor);
artistCardIntent.putExtra("artist_id", artist.getId());
artistCardIntent.putExtra("tt", "tt");
startActivity(artistCardIntent);
};
};
And while debugging I can see that artistCardIntent gets populated, however in the receiving activity Intent i doesn't contain any extra inforamation:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.artist_card);
Intent i = getIntent();
What am I doing wrong?
PS both activities extends FragmentActivity.
Thanks.
To receive the data use getextras() method. Find the following code. i think it helps you
Intent i = getIntent();
Bundle b=i.getExtras();
if(b!=null)
{
String artist= ("artist_id");
}
I don't see that you are getting the extra information.
Check out this answer How do I get extra data from intent on Android?
convert those values to string and try it , It will work
startActivity(new Intent(Home.this,galmenu.class).putExtra("page","home"));
Intent myIntent = getIntent();
test=myIntent.getExtras().getString("page");
it is working perfectly
Related
I have an app that for right now, consists of 2 buttons (will later consists of 20+). When I click on a button, it takes me to a new activity that has a list of items I can select. After selecting something and clicking the Back button, it starts a new activity that passes on the item's information (in this case, "orange") and then it assigns the word "orange" to the button that was clicked.
Now when I click on the other button to assign it's information, I lose all of my first button information. What are my options for saving the previous information? Would I have to create an intent for it and keep passing it back and forth between actvities?
At the end, I need to collect all the information that was assigned to both buttons and pass that onto another activity, as this is just the customizing page. Is there a way I can just have the Strings set such that leaving the activity won't delete the String information?
Here's my MainActivity
Bundle extras = getIntent().getExtras();
if (extras != null) {
btnValue = extras.getString("btnValue");
itemValue = extras.getString("itemValue");
}
if (btnValue.equals("btn1")){
btn1.setText(itemValue);
} else if (btnValue.equals("btn2")) {
btn2.setText(itemValue);
}
}
public void onClickBtn1(View v) {
Intent myIntent = new Intent(this, Main2Activity.class);
myIntent.putExtra("btn", "btn1");
startActivity(myIntent);
}
public void onClickBtn2(View v) {
Intent myIntent = new Intent(this, Main2Activity.class);
myIntent.putExtra("btn", "btn2");
startActivity(myIntent);
}
and my 2nd activity
Bundle extras = getIntent().getExtras();
if (extras != null) {
btnValue = extras.getString("btn");
}
listView = (ListView) findViewById(R.id.list);
String[] values = new String[] { "apple", "banana", "orange", "cherry"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
itemPosition = position;
itemPositionString = String.valueOf(itemPosition);
}
});
}
public void onClickBack (View v) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("btnValue", btnValue);
intent.putExtra("itemValue", itemValue);
startActivity(intent);
}
You can use the Android lifecycle to manage the activity's state.
To save the activity state you need to do your work on the method onSaveInstanceState.
onSaveInstanceState(Bundle bundle)
On restoration you either check the bundle the following methods
onRestoreInstanceState(Bundle bundle)
onCreate(Bundle bundle)
You can find more details here:
https://developer.android.com/training/basics/activity-lifecycle/recreating.html
When you use startActivity it creates new activity. So you lose old information. You need to use startActivityForResult from MainActivity while starting second activity and from the second activity you should use setResult
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK, returnIntent);
finish();
And you handle the result in MainActivity with
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
..
..
}
Here, there is an example How to manage `startActivityForResult` on Android?
You basically have 3 choices:
Pass all data in the Intent and return any new/changed data in another Intent using startActivityForResult().
Save the data in a static variable somewhere (globals). All activities can then reference the current data and make changes to it that are then seen by all other activities. This is the quick-and-dirty solution which is suitable for small, trivial or "homework" solutions.
Save the data in a persistent storage (a file or an SQLite database). All activities can read all the current data, display it and make changes. After the BACK button is pressed, the underlying Activity should read the current data from the persistent storage to refresh the views.
Hi in android i know how to send image from one activity to another and send text from text view to another,separately. But i want to know in an activity we have both text view and image view.But my need is i want to send both text and image from one activity to another.Any suggestions is welcomed.
Thank you.
What do you mean by sending along an image? For passing a string, use Intent extras and Bundle.
In your first activity...
someButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("text_contents", someTextView.getText().toString();
startActivity(intent);
}
});
In the second activity onCreate(), you retrieve the intent extras you passed via Bundle...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String someString = bundle.getString("text_contents");
}
}
If your "image" is an R.drawable resource, then you could simply add that as well to the intent extras:
intent.putExtra("image_resource", R.drawable.some_image_resource);
And retrieve it from the Bundle like:
int someImageResource = bundle.getInt("image_resource");
And from there you can apply it to some ImageView:
someImageView.setImageResource(someImageResource);
EDIT: made a slight correction + if your "image" is a bitmap, then see Anjali's answer.
Question is simple, I am not exactly new to Android but I cannot, for the life of me, retrieve the extras passed via an intent from Activity A to Activity B.
See Activity A: This is actually a ListFragment, that implements onListItemClick() to start another activity via an intent.
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("FragmentList", "Item clicked: " + id);
Intent i = new Intent(getActivity(), ExpandedTweetView.class);
twitter4j.Status status = adapter.getItem(position);
Bundle extras = new Bundle();
extras.putString(KEY_TEXT, status.getText());
extras.putString(KEY_HANDLE, status.getUser().getScreenName());
extras.putString(KEY_NAME, status.getUser().getName());
extras.putString(KEY_TIMESTAMPS, status.getCreatedAt().toString());
extras.putLong(KEY_RETWEETS, status.getRetweetCount());
i.putExtra(KEY_EXTRAS, extras);
startActivity(i);
}
This part just works, I tested it usng Log.v(TAG, "status.getText()" to make sure that the error was not coming from the Adapter passing an empty item via getItem().
Here is the code on Activity B:
public class ExpandedTweetView extends Activity {
TextView text;
TextView name;
TextView handle;
TextView createdAt;
TextView retweets;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expanded_list_item);
Bundle extras = getIntent().getExtras();
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
text = (TextView) findViewById(R.id.h_content);
name = (TextView) findViewById(R.id.h_name);
handle = (TextView) findViewById(R.id.h_handle);
createdAt = (TextView) findViewById(R.id.h_timestamp);
retweets = (TextView) findViewById(R.id.h_retweet_count);
if(extras != null) {
text.setText(extras.getString(TimelineFragment.KEY_TEXT));
name.setText(extras.getString(TimelineFragment.KEY_NAME));
handle.setText(extras.getString(TimelineFragment.KEY_HANDLE));
createdAt.setText(extras.getString(TimelineFragment.KEY_TIMESTAMPS));
retweets.setText(String.valueOf(extras.getLong(TimelineFragment.KEY_RETWEETS)));
}
}
As you can see, I believe I am using the right code to obtain the extras, using the same code on other applications worked. Not sure why, when the ExpandedTweetView is created via an intent, ALL of the textViews are empty. See: https://www.dropbox.com/s/pso6jbyn6rpks9n/empty_activity.png
What is even MORE strange is that I had initially tried checking to see if the bundle was null by calling this:
if (extras == null) {
Log.v(TAG, "Extras are empty :(");
}
But that line was never executed, meaning the bundle is not null. I also thought that maybe the keys being used to retrieve the individual Strings from the bundle were mismatching; however, in order to remedy that I decided to create constants that could be used on both sides. As you can see on the code, both the key to set the Extra and the Key to retrieve the Extra are the same.
Any ideas as to what the heck is going on?
Bundle extras = getIntent().getExtras();
if (extras != null) {
extras = extras.getBundle("KEY_EXTRAS");
String status = extras.getString("KEY_TEXT");
}
Try adding the extra variable to intent rather than in Bundle
Ex:
i.putExtra(KEY_1, a);
i.putExtra(KEY_2, b);
i.putExtra(KEY_3, c);
Then retrieve it from other activity from intent
Ex:
getIntent().getStringExtra(KEY_1) ;
In Activity A:
Intent i = new Intent(MainActivity.this, AnotherActivity.class);
Bundle b = new Bundle();
b.putString("thisc", "my name");
i.putExtra("bundle", b);
startActivity(i);
In Activity B:
**Bundle bun = getIntent().getBundleExtra("bundle");
if (bun.containsKey("thisc")) {
Log.i("TAG", bun.getString("thisc"));
} else {
Log.i("TAG", "no thisc");
}**
Check the first line of code in Activity B, that's the main difference actually!!
//put value
Intent inatent = new Intent(this,text.class);
inatent_logo.putExtra("message","hello");
startActivity(inatent);
//get vale
String msg = getIntent().getStringExtra("message").toString();
It's Difficult to maintain intent to Bundle and Bundle to Intent if number of data you want ti share from one Activity to Other Activity.
just Simply use Intent with PuExtra() with different argument.
you can pass number of data in intent like :
Sender's Side :
Create your Intent.
Intent My_Intent = new Intent(FromClass.this,ToClass.class);
Add your value which you want to share with other activity.
My_Intent.putExtra("VAR_A",a_value);
My_Intent.putExtra("VAR_B",b_value);
My_Intent.putExtra("VAR_C",c_value);
Call your Intent.
StartActivity(My_Intent);
Receiver's Side :
Intent My_Intent = getIntent();
First_Value=My_Intent.getStringExtra("VAR_A");
Sec_Value=My_Intent.getStringExtra("VAR_B");
Thred_Value=My_Intent.getStringExtra("VAR_C");
I Think its Easy for you to Handel your data from one Activity to other .
As the title says I want to transfer data, in this case the information introduced by the user on an EditText and a Spinner, from one activity to another.
I am following a tutorial from a book but it doesn't work (I think its not complete). Here the code of the program:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
this.location=(EditText)findViewById(R.id.location);
this.cuisine=(Spinner)findViewById(R.id.cuisine);
this.grabReviews=(Button)findViewById(R.id.get_reviews_button);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.cuisine, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
this.cuisine.setAdapter(adapter);
this.grabReviews.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
handleGetReviews();
}
}
);
}
private void handleGetReviews() {
RestaurantsActivity application= (RestaurantsActivity) getApplication();
application.setReviewCriteriaCuisine(this.cuisine.getSelectedItem().toString());
application.setReviewCriteriaLocation(this.location.getText().toString());
Intent intent=new Intent(Constants.INTENT_ACTION_VIEW_LIST);
startActivity(intent);
}
This code above doesn't work. I dont understand four things:
-RestaurantsActivity must be the actual activity right?
-In all the examples I have seen over the internet there is an application extends class, in this example there isnt.
-setReviewCriteria function is missing
-Where does Constants.INTENT_ACTION_VIEW_LIST come from ?
So your target is to get the data to Restaurantsactivity?
Normally data in android are handed over from one activtiy to another by using Intents.
So first you create an intent.
Then you put the data you want to transfer into the intent by using the intent.putExtra() method.
In the activity that gets the intent you can get the data by using getIntent().getExtra() method (getExtra can be something like getStringExtra()).
Here is a small example for a edit box called "name":
public void onCreate(Bundle iBundle){
//do some stuff here
//perhaps define some Buttos and so on
//now lets start the activity
Intent intent = new Intent(currentActivityname.this, ActivityYouWantToStart.class);
intent.putExtra("name", name.getText().toString())
startActivity(intent); // you can also start an startActivityForResult() here :)
}
In our receiving activity you can now handle the intent (e.g. in the onCreate() method
public void onCreate(Bundle iBundle){
String name = this.getIntent().getStringExtra("name",some default value);
}
Try to put data in the Bundle and start Activity with this Bundle
Intent intent = new Intent(this, YourSecondActivity.class);
intent.putExtra(... HERE YOUR BUNDLE WITH DATA ...);
startActivity(intent);
Hope, it help you!
I'm getting problems using the Intent for navigate through the screens. I want to send a variable and use it in the other class.
I'm using a method, the method takes the variable but i don't know how to send it with the intent to the new screen, which will use it to do some things.
Main class calls the metod:
private void pantallaDetalles(final int identificador)
{
startActivityForResult(new Intent(this,MostrarDetalles.class),REQST_CODE);
}
MostrarDetalles.class is the *.java which will take the variable. I'm begining it like this:
public class MostrarDetalles extends Activity {
SQLiteDatabase db;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.detalles);
//more code...
Cursor c = db.rawQuery("SELECT * FROM table WHERE _id="+ identificador, null);
}
Did you see? I'm talking about this. I don't know how to send the "identificador" variable from the main class to the second class through the Intent.
Can you help me with this? Thank you very much in advice.
JMasia.
Use the extras bundle in the intent.
Intent i = new Intent(...);
i.putExtra("name_of_extra", myObject);
Then on onCreate:
getIntent.getIntExtra("name_of_extra", -1);
Screen 1:
Intent i=new Intent("com.suatatan.app.Result");
i.putExtra("VAR_RESULT", "Added !");
startActivity(i);
Screen 2: (Receiver):
TextView tv_sonuc = (TextView) findViewById(R.id.tv_sonuc);
Bundle bundle = getIntent().getExtras();
String var_from_prev_intent = bundle.getString("VAR_RESULT");
tv_sonuc.setText(var_from_prev_intent);
You can use Intent.putExtra() to bundle the data you want to send with the intent.