Passing values from Activity to Fragment - android

I'm using Navigation drawer after login in my app. In Navigation drawer I am using a fragment called "profile" for displaying the user information. I want to passing data from loginpage activity to the profile fragment.
Bundle bundle = new Bundle();
Intent home = new Intent(LoginPage.this, HomeActivity.class);
startActivity(home);
bundle.putString("name", gname);
Profile profile = new Profile();
profile.setArguments(bundle);
And this is my Profile Fragment:
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
name = this.getArguments().getString("name");
ntext.setText(name);
return inflater.inflate(R.layout.activity_profile, container, false);
}
But I am getting Null Pointer Exception. I can't understand what is the problem! If there are another way to pass Data from activity to fragment, please tell me!

You need to create a function in your Profile fragment called newInstance that creates the fragment and set the arguments through there, then returns the fragment with the arguments. Like this
public static Profile newInstance(String name){
Profile profile = new Profile();
Bundle bundle = new Bundle();
bundle.putString("name", name);
profile.setArguments(bundle);
return profile;
}
Then create the fragment in your activity like this
Profile profile = Profile.newInstance(gname);
And get the arguments how you have been doing in the onCreate in the fragment.
You also should be creating the Fragment in the activity you are using it in. So if it's in your home activity you will want to pass the data from the login activity, then build the fragment in the onCreate for the home activity.
Intent home = new Intent(this, HomeActivity.class);
intent.putExtra("name", gname);
startActivity(home);
in the HomeActivity
Bundle extras = getIntent().getExtras();
String gname = extras.getString("name");
Profile profile = Profile.newInstance(gname);

In Kotlin, you can access an activity variable from a fragment like this
(activity as ActivityClassName).variableName
I don't have much experience in Java, but I believe this is that code translated into Java
((ActivityClassName) getActivity()).variableName

Related

How to use a bundle for sending data to fragment from an activity in Android Studio

I am trying to build simple movies name list in recyclerview inside drawer layout when I click any movie name show me the detail of movie like an image, textView in another fragment. How to implement a bundle for sending data to fragment from an activity.This is my main activity
You can send data from your activity to fragment like this:
In your MainActivity , add fragment when you want to open detail fragment
addFragment(R.id.frame_container,MovieDetailFragment.getInstance(/*your data*/),tag_name);
//then commit
In your Movie Detail Fragment,
public static MovieDetailFragment(//your data//){
MovieDetailFragment detailFragment new MovieDetailFragment();
Bundle bundle = new Bundle();
bundle.putString(key,//yourdata//);//you can use any type you want to put in bundle
detailFragment.setArguments(bundle);
return movieDetailFragment;
}
and in your MovieDetailFragment's onCreateView() , you can get your data from bundle like this:-
if(getArguments()!=null){
String data = getArguments().getString(key);
}
I hope it will solve your query!!
Step 1: Passing the data from activity to fragment,
Bundle bundle = new Bundle();
bundle.putString("params", "My String data");
set MyFragment Arguments
MyFragment myObj = new MyFragment();
myObj.setArguments(bundle);
Step 2: Receiving the data to the fragment,
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString("params");
}
}

How to pass data from one activity to other activity fragment in android? [duplicate]

This question already has answers here:
Send data from activity to fragment in Android
(22 answers)
Closed 5 years ago.
I have passed some key and value data from one activity to another activity fragment so I have not get key and value to the last point in the fragment.
I have passing data using bundle.
In your activity create bundle to set to the fragment
Yourfragment fragment = new Yourfragment();
Bundle args = new Bundle();
args.putString(ARG_DATA, data);
fragment.setArguments(args);
Then load the fragment getSupportFragmentManager().beginTransaction().replace(R.id.your_container,fragment).commit();
Then in your fragment oncreate get the data like this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mData = getArguments().getString(ARG_DATA);
}
}
From activity to activity you can pass data by using Intent and when you get data in second activity in which you are creating fragments on creating fragment pass that data to fragment by making constructor in fragment or by using bundle. For further assistance and if you dont know how to do this do let me know.
Pass data from activity to activity:
Intent intent = new Intent(this, Second.class);
intent.putExtra("data", sessionId);
startActivity(intent);
get data in Second activity:
String s = getIntent().getStringExtra("data");
Pass data from activity to fragment:
Bundle bundle = new Bundle();
bundle.putString("data", "From Activity");
// set Fragmentclass Arguments
FragmentOne fragment = new FragmentOne ();
fragment.setArguments(bundle);
get data from activity to fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("data");
return inflater.inflate(R.layout.fragment, container, false);
}
Happy coding!!
You can pass data between Activity and Fragments and Fragment to fragment using below:
https://developer.android.com/training/basics/fragments/communicating.html
But to pass it between activity, you may use bundle data, (very few variables) or use Application Class to store it in memory, make sure you do not bloat up your memory.
New Android architecture components also do provide good options, it all depends upon your use:
https://developer.android.com/topic/libraries/architecture/index.html

Receive data from class to fragment by Intent

I want to receive data from class to fragment by Intent ,I try to do ,I write
Intent n = this.{getIntent()};
{the wrong here} ,but this code is not working ,so what i do ?
Activity Send Intent data to fragment
Bundle b = new Bundle();
b.putString("data", "abc");
// set Fragmentclass Arguments
Fragment frag = new Fragment();
frag.setArguments(b);
Fragment receive data with intent
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String data = getArguments().getString("data");
return inflater.inflate(R.layout.fragment, container, false);
}
For fragments, you can visit Send data from activity to fragment in android
Try this,
you can send data from activity using,
Bundle bundle = new Bundle();
bundle.putString("key", "value");
Fragmentclass fc= new Fragmentclass();
fc.setArguments(bundle);
and receive from this way in your fragment's onCreateView method,
String strtext = getArguments().getString("key");
that's it

Passing data from one fragment to another

I have an Activity with two fragments and I need to pass a string from FragmentA to FragmentB.
To pass the data, I have this in my FragmentA:
Intent intent = new Intent(getActivity(), FragmentB.class);
intent.putExtra("name", "Mark");
startActivity(intent);
And to get the data, I did this in FragmentB
Intent intent = getActivity().getIntent();
Bundle b = intent.getExtras();
if(b!=null)
{
String name =(String) b.get("name");
nameTextView.setText(name);
}
But this is not working. Is there a specific way to pass a string from one fragment to another fragment?
If fragments are hosted by same activity-
You cannot cast an intent to Fragment. Fragment acts as a part of Activity, it is not an activity by itself. So to share a string between fragments you can declare a static String in Activity. Access that string from Fragment A to set the value and Get the string value in fragment B.
Both fragments are hosted by different Activities-
Then you can use putExtra to pass a string from Fragment A of Activity A to Activity B. Store that string in Activity B and use it in Fragment B.
to pass data between Fragments you can use setArguments(Bundle b). For instance:
public class FragmentA extends Fragment {
public static FragmentA newInstance(String name) {
Bundle bundle = new Bundle();
bundle.putString("name", name);
FragmentA f = new FragmentA();
f.setArguments(bundle);
return f;
}
}
You can do something like below,
Fragment fr=new friendfragment();
FragmentManager fm=getFragmentManager();
android.app.FragmentTransaction ft=fm.beginTransaction();
Bundle args = new Bundle();
args.putString("CID", "your value");
fr.setArguments(args);
ft.replace(R.id.content_frame, fr);
ft.commit();
To receive the data do the following,
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("CID");
return inflater.inflate(R.layout.fragment, container, false);
}
Code in FragmentActivity 1 fragment A:
fb is the instance of the bean I created and ID is the parameter
Intent intent = new Intent(getContext(), FragmentActivity2.class);
intent.putExtra("id", fb.ID);
startActivity(intent);
Code in FragmentActivity 2 fragment Any:
fragmentA= (FragmentActivity2) getActivity();
String cust_id = fragmentA.getIntent().getExtras().getString("id");

Android: pass a value from container Activity to its fragment

My code contains a main Activity and three fragments inside it and I want to pass a value from the container activity to its fragment, but it's not working.
I tried to make an interface to communicate to each other but nothing happened.
I also tried to make a bundle but I have an error in setArguments.
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);
and in the fragment
Bundle bundle = this.getArguments();
if(bundle != null){
int i = bundle.getInt(key, defaulValue);
}
Can you please help me?
Thank you!
Try with an Intent in your activity :
Intent a = new Intent (this, yourfragment.class);
a.putInt(key, value);
setIntent(a);
And in your fragment :
ActivityName activity = (ActivityName) getActivity();
Intent b= activity.getIntent();
int Uid = b.getIntExtra(key);

Categories

Resources