After seeing this question, it got me thinking. I can get a Intent in a Fragment by calling this inside onCreateView:
String Item = getActivity().getIntent().getExtras().getString("name");
the problem with this is that getActivity might return null, to counter that I can call:
if(getActivity() != null)
String Item = getActivity().getIntent().getExtras().getString("name");
}
this will work fine, but..
I was thinking of creating a static method in my Activity and then accessing the Intent in my fragment by calling that method, like this (In my Activity):
public class DemoActivity extends Activity{
static String name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo);
//Getting the Intent from the previous Activity
name = getIntent().getStringExtra("name");
}
public static String Name(){
//returning the Intent
return name;
}
}
Then in my Fragment I can call this like this:
String name = DemoActivity.Name();
My Question:
Can I do it like this? Will it cause any issues and why?
Currently
It is working fine.
try like this:
Activity class:
Bundle bundle = new Bundle();
bundle.putString("your_key", "your_value");
your_fragment.setArguments(bundle);
Fragment class:
String your_variable = getArguments().getString("your_key");
Set in first activity fragment:
Bundle bundle = new Bundle();
bundle.putString("your_string_key", "your_value");
startActivity(new Intent(getActivity() your_second_activity.class).putExtra("bundle_key", bundle));
Get bundle value second activity:
fragment.setArguments(getIntent().getBundleExtra("bundle_key"));
In Second Activity Fragment:
getArguments().getString("your_string_key")
Related
I'm clumsy.
solution on my problem
origin activity
public void marcas(View view) {
ArrayList<Localizacion> object = new ArrayList<Localizacion>(localizaciones);
Intent intent = new Intent(getApplicationContext(), CompraVenta.class);
Bundle args = new Bundle();
args.putSerializable("ARRAYLIST", (Serializable) object);
intent.putExtra("BUNDLE", args);
startActivityForResult(intent, RESPUESTA_ACTIVIDAD);
}
destiny activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_compra_venta);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent intent = getIntent();
Bundle args = intent.getBundleExtra("BUNDLE");
ArrayList<Localizacion> object = (ArrayList<Localizacion>) args.getSerializable("ARRAYLIST");
loc = object;
enlaceInterfaz();
}
in the class Localizacion i'm implement Parceable.
my mistake was to want to use the arrays like I use them in the java class and that's not how it works.
thanks for all
Make your class as Parceable, and then you will be able to pass the list of objects of that class to another Activity.
class Localizacion implements Parceable {
}
Now to send data to another Activity.
intent.putParcelableArrayListExtra("array",object);
In receiving side
loc = getIntent().getParcelableArrayExtra("array");
I want to send a string from a recyclerView of Activity1 to a fragment of Activity2 using intent. for better understanding, here is the demonstration image
so I'm fetching the key from recyclerView like this
#Override
protected void populateViewHolder(EventsViewHolder viewHolder, EventDetails model, int position) {
viewHolder.setEventDate(model.getDate());
viewHolder.setEventIcon(getApplicationContext(),model.getIcon());
viewHolder.setEventTitle(model.getTitle());
viewHolder.setEventDescription(model.getDescription());
viewHolder.setEventTotalGuest(model.getTotal_guests());
String guest_key = getRef(position).getKey();
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent guestListIntent = new Intent(MainActivity.this, GuestListActivity.class);
startActivity(guestListIntent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
});
}
and now I want to send the guestKey by using intent from this activity to a fragment of GuestListActivity.class
I've tried Bundle but I can't send data.
TIA
First, send data from activity1 to activity2
Intent guestListIntent = new Intent(MainActivity.this, GuestListActivity.class);
guestListIntent.putExtra("guest_key",guest_key)
startActivity(guestListIntent);
Catch it in Activity 2
String guest_key = "";
Bundle bundle = getIntent().getExtras();
if(bundle != null){
guest_key = bundle.getString("guest_key","");
}
Now send to Fragment of Activity 2
Bundle bundle = new Bundle();
bundle.putString("guest_key", guest_key);
NewFragment newFragment = new NewFragment();
newFragment.setArguments(bundle);
Catch in Fragment onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String guest_key = getArguments().getString("guest_key","");
return inflater.inflate(R.layout.fragment_item_three, container, false);
}
send the data as u send from activity to activity and receive in fragment as getActivity().getIntent() and then do as you receive data in activity.
Here, any fragment of Activity2 is being created after onCreate() of this activity.
I will suggest you to use any of two approach to receive data in a Fragment of Activity2.
Approach 1:
Send data using Bundle with intent and receive it in Activity2 then set as Arguments while making transition of a fragment.
Bundle bundle = new Bundle();
bundle.putString("YOUR_KEY", "KEY_VALUE");
guestListIntent.putExtras(bundle);
startActivity(guestListIntent);
Receive this data in Activity2,
Bundle extras = getIntent().getExtras();
Set data while Fragment transition,
Fragment frag = new YOUR_FRAGMENT();
frag.setArguments(extras);
Receive data in Fragment,
String value= getArguments().getString("YOUR_KEY");
Approach 2:
Declare static variable. Assign value inside onClick then access and use this value from fragment class
You have to send the data to GuestListActivity and retrive the data in GuestListActivity. After that when you do the fragment tracation or add the fragment pass the data through bundle. Then you will be able to get the data in your desire fragment. For better understanding see the demonstration image.
send data to GuestListActivity
#Override
protected void populateViewHolder(EventsViewHolder viewHolder, EventDetails model, int position) {
// other code of view holder
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent guestListIntent = new Intent(MainActivity.this, GuestListActivity.class);
guestListIntent.putExtra("guest_key",guest_key)
startActivity(guestListIntent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
}
}
Retrieve the data in GuestListActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
String guest_key = "";
Bundle bundle = getIntent().getExtras();
if(bundle != null){
guest_key = bundle.getString("guest_key","");
}
}
Send the data to your fragment
Bundle bundle = new Bundle();
bundle.putString("guest_key", guest_key);
MyFragment myFragment = new MyFragment();
myFragment.setArguments(bundle);
Retrieve data in your fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String guest_key = getArguments().getString("guest_key","");
}
I have 2 Fragments that both pass an intent to an Activity (through an event listener). How can the Activity know which of these 2 fragments passed the intent? There's a method called getCallingActivity(), I need the equivalent for fragments.
I Attempted to determine which Fragment Called the Activity with the onAttachFragment() method, But it doesn't work:
public class DetailsActivity extends Activity {
static final String POSITION = "position";
private Movie movie;
private int position;
private static final String TAG = "app";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
}
#Override
public void onAttachFragment(Fragment fragment) {
super.onAttachFragment(fragment);
Log.v(TAG, "fragment_src");
position = (int) getIntent().getExtras().get(POSITION);
if (fragment instanceof PopularFragment) {
movie = PopularFragment.movieList.get(position);
setData();
} else if (fragment instanceof TopRatedFragment) {
movie = TopRatedFragment.movieList.get(position);
setData();
}
}
public void setData() {
TextView original_title = (TextView) findViewById(R.id.original_title);
original_title.setText(movie.getOriginal_title());
...
}
}
You could send different int values with your intent for both fragment and check in your activity..or you could get your fragment by using
Fragment fragment = getFragmentManager().findFragmentByTag("yourtag");
Why don't you pass some parameters through the Intent itself
For first fragment-
Intent intent=new Intent(getActivity(),DetailsActivity.class);
intent.putExtra("fragName","PopularFragment");
startActivity(intent);
For Second Fragment-
Intent intent=new Intent(getActivity(),DetailsActivity.class);
intent.putExtra("fragName","TopRatedFragment");
startActivity(intent);
In your Activity
String fragName=getIntent().getStringExtra("fragName");
And then just check using if..else.
/*I have problem with intent that i can't settext which i have got from getIntent from previous activity*/
public class Done extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_done);
TextView text = (TextView)findViewById(R.id.donetxt);
int tappedapples=0;
RelativeLayout rl = (RelativeLayout)findViewById(R.id.RelativeLayout);
rl.setBackgroundResource(R.drawable.background);
/*I want to setText which i have got from previous activity*/
Bundle extras = getIntent().getExtras();
if(extras!=null){
int apples = extras.getInt("tappedapples",0);
text.setText(Integer.parseInt("tappedapples"));
}
}
}
/I have problem with intent that i can't settext which i have got from getIntent from previous activity/
Change this:
int apples = extras.getInt("tappedapples",0);
text.setText(Integer.parseInt("tappedapples"));
To:
int apples = extras.getInt("tappedapples",0);
text.setText(apples+"");
I have two class Profile.class and Details.class,
In profile class i have used a spinner with values like (ATM,Banking,Personal,Others etc)
and a button (OK).
on clicking ok button it will go to next activity that is details activity where i will be taking some details like-name,description etc.
after filling the details i have given a button (save).
on clicking button save i will be saving the name and description in database but i want to save the profile name also along with details. i am unable to transfer selected spinner text from Profile.class to Details.class
how to transfer?
create.class code
public class Create extends Activity {
public ArrayList<String> array_spinner;
Button button4;
String spinnertext;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
array_spinner=new ArrayList<String>();
array_spinner.add("ATM");
array_spinner.add("Bank");
array_spinner.add("Mail");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, array_spinner);
adapter.setNotifyOnChange(true);
spinner.setAdapter(adapter);
spinner.setLongClickable(true);
spinner.setOnLongClickListener(new OnLongClickListener(){
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return false;
}}
);
button4 = (Button)findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent4 = new Intent(view.getContext(), Details.class);
startActivityForResult(myIntent4, 0);
myIntent4 .putExtra("key", array_spinner.getSelectedItem().toString());
startActivity(myIntent4);
}
});
}}
details.class code
public class Details extends Activity {
EditText editText4,editText5,editText6;
Button button8,button9,button10;
TextView textView7;
String et4,et5,et6;
//SQLite Database db;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
String spinnervalue = getIntent().getExtras().getString("Key");
please kindly explain me what is this "key"?
You can use :
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("YourValueKey", yourData.getText().toString());
then you can get it from your second activity by :
Intent intent = getIntent();
String YourtransferredData = intent.getExtras().getString("YourValueKey");
example
this is what you have to write in your first activity
Intent i = new Intent(getApplicationContext(), Product.class);
i.putExtra("productname", ori);
i.putExtra("productcost", position);
i.startActivityForResult(i,0);
then in your next activity you need to have this code
String productname,productcost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.product);
tv1= (TextView)findViewById(R.id.tv1);
tv2= (TextView)findViewById(R.id.tv2);
Bundle extras= getIntent().getExtras();
if(extras!=null)
{
position = extras.getString("position"); // get the value based on the key
tv1.setText(productname);//use where ever you want
productname = extras.getString("productname"); // get the value based on the key
tv2.setText(productname);
}
First of all take a spinner and provide value to them what you want and then the selected spinner value change it to string value and this string variable will be used in OK button to pass value through use of Intent or Shared preference to take this value to another activity and through there you can use it in database to display this value.
If you want to send data to another activity, you can do it using intent.
Bundle bund = new Bundle();
bund.putString("myKey",name);
Intent intent = new Intent(Profile.this, Detail.class);
intent.putExtras(bund);
startActivity(intent);
Now in Detail class, receive this data in onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
.......
String nameReceived = getIntent().getExtras().getString("myKey");
}
I have given the example of passing String to another activity however, you can pass boolean, int, double etc to another activity. See the full list on here