Example
I do not understand this example.
How to apply in my program
I try to write In my program
General use that I can.
But replaced fragment.
I do not know how to use.
Pass the value of the object.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.templistmune, container, false);
FragmentManager DS2 = getFragmentManager();
FragmentTransaction DSE2 = DS2.beginTransaction();
Fragment DF2 = DS2.findFragmentById(R.id.frameLayout4);
if (DF2 == null) {
String title = "Fragment A";
templistview2 usermune = new templistview2(title);
DSE2.add(R.id.frameLayout4, usermune);
DSE2.addToBackStack(null);
DSE2.commit();
/////////////////////////////////////////////////////////
Bundle bundle = new Bundle();
String SAS="50";
bundle.putString("ST", SAS);
DF2.setArguments(bundle);
////////////////////////////////////////////////
}
The pick value the object
templistview2.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.templistmune2, container, false);
////////////////////////////////////////////////////////////////////////////////////
Bundle bundle = this.getArguments();
String myST = bundle.getString("ST", SAS);
///////////////////Error SAS cannot be resolved to a variable /////////////////
return view;
}
You're only adding adding the Bundle when DF2 is null... so your program will either crash with a NullPointerException somewhere along the line or run but not have the Bundle passed.
So place
Bundle bundle = new Bundle();
String SAS="50";
bundle.putString("ST", SAS);
DF2.setArguments(bundle);
outside the if structure.
Related
--This is my Fragment :--
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_save, container, false);
savedAlamat = view.findViewById(R.id.saved_alamat);
savedContac = view.findViewById(R.id.saved_kontak);
savedName = view.findViewById(R.id.saved_nama);
String malamat = getArguments().getString("mAlamat");
String mcontac = getArguments().getString("mContac");
String mname = getArguments().getString("mname");
savedAlamat.setText(malamat);
savedContac.setText(mcontac);
savedName.setText(mname);
return view;
}
//and This is main acivity that constarin framelayout :
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_save, container, false);
savedAlamat = view.findViewById(R.id.saved_alamat);
savedContac = view.findViewById(R.id.saved_kontak);
savedName = view.findViewById(R.id.saved_nama);
String malamat = getArguments().getString("mAlamat");
String mcontac = getArguments().getString("mContac");
String mname = getArguments().getString("mname");
savedAlamat.setText(malamat);
savedContac.setText(mcontac);
savedName.setText(mname);
return view;
}
--I don't understand why I keep get NotNULLobject, i want to make a passing data between that activity and fragment.
In Activity, write this code
Bundle bundle = new Bundle();
bundle.putString("mAlamat", "Data to send"); // set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);
In Fragment class, receive data like this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("mAlamat");
return inflater.inflate(R.layout.fragment, container, false);
}
Courtesy
I am new developer of Android.How we can share the data between two fragment.
Any idea for this problem.i have make the orderFragment and itemfragment user select the item on item screen and press submit the screen redirct to orders screen user have multiple order.if we redirect the screen the data not send to the orders screen
For Send data between two fragment try below code
YourSubmitButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Bundle bundle = new Bundle();
bundle.putString = ("KEY",value) //Here if you want to send Integer value then write putInt
YourItemFragment.setArguments(bundle);
}
}
In ItemFragment try below code
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_order, container, false);
String YourItem = getArguments.getString("KEY");
return view;
}
Its simple to send data between fragments try this
Bundle bundle = new Bundle();
bundle.putString("YourKeyHere",value);
itemFragment.setArguments(bundle);
And then in order Fragment receive as
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String item = getArguments().getString("YourKeyHere");
return inflater.inflate(R.layout.fragment, container, false);
}
i must pass two parameters from fragment to another fragment. First parameter is type String and second parameter is type int. i want pass parameters with bundle but doesn't work.
FIRST FRAGMENT
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt("totale", totalAmount);
fragment.setArguments(bundle);
SECOND FRAGMENT
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
TextView titolo2 = (TextView) getView().findViewById(R.id.quantità 2);
Bundle bundle=this.getArguments();
int myInt = bundle.getInt("totale", 0);
titolo2.setText(myInt);
return inflater.inflate(R.layout.fragment_two, container, false);
}
Change Second Fragment like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_two, container, false);
TextView titolo2 = rootView.findViewById(R.id.quantità 2);
Bundle bundle=this.getArguments();
int myInt = bundle.getInt("totale", 0);
titolo2.setText(myInt + "");
return rootView;
}
In first fragment write this:
TwoFragment fragment = new TwoFragment ();
Bundle bundle = new Bundle();
bundle.putInt("totale", totalAmount);
fragment.setArguments(bundle);
Second fragmnent:
Bundle bundle=getArguments();
int myInt = bundle.getInt("totale", 0);
titolo2.setText(myInt);
Create a public class with two static members like this :
public class SomeClass {
public static String stringValue="";
public static int integerValue=0;
}
Then from your first fragment just set values like this :
SomeClass.stringValue="yourValue";
SomeClass.integerValue=yourIntegerValue;
You can use these variable from your second fragment where you want :
int a =SomeClass.integerValue;
String str=SomeClass.stringValue;
If both fragments belong to the same activity, better to use this approach:
In activity you should save references to fragments, and all comunications between this fragments should be through activity, let's name it BaseActivity. For example add to activity method which will pass params to second fragment:
void updateSecondFragment(int param1, String param2){
mFragment2.updateTitle(param2);
}
In second fragment:
void updateTitle(String title){
mTextView.setText(title);
}
And in first fragment:
BaseActivity mActivity;
#Override
void onAttach(Activity activity) {
if (activity instanceOf BaseActivity){
mActivity = (BaseActivity) activity;
}
}
private void updateSecondFragment(){
if (mActivity != null){
mActivity.updateSecondFragment(int param1, String param2);
}
}
You should do the work of second fragment as:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_two, container, false);
}
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView titolo2 = view.findViewById(R.id.quantità 2);
Bundle bundle=getArguments();
int myInt = bundle.getInt("totale", 0);
titolo2.setText(String.valueOf(myInt));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
et=(EditText)rootView.findViewById(R.id.et1);
String str=et.getText().toString();
Fragment frag=new Fragment();
Bundle bundle1=new Bundle();
bundle1.putString("name", str);
frag.setArguments(bundle1);
getFragmentManager().beginTransaction().add(R.id.container_body, frag).commit();
return inflater.inflate(R.layout.fragment_currentmovie, container, false);
}
}
it is displaying rootView cannot be resolved,i need to add edit text et to string str so that using bundle i can pass it to the next fragment
Try this Add inflater then return.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_currentmovie, container, false);
et=(EditText)rootView.findViewById(R.id.et1);
String str=et.getText().toString();
Fragment frag=new Fragment();
Bundle bundle1=new Bundle();
bundle1.putString("name", str);
frag.setArguments(bundle1);
getFragmentManager().beginTransaction().add(R.id.container_body, frag).commit();
return rootView ;
}
}
You need to inflate it first:
View rootView = inflater.inflate(R.layout.fragment_currentmovie, container, false);
...
return rootview;
So I am finally learning Fragments after learning Activities. I am looking at a video tutorial. All of a sudden he changed the way a Fragment inflates a layout. Why does he have two different ways? The first way in FragmentA is:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_a, container, false);
}
Then the other way in FragmentB is:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_b, container, false);
if (savedInstanceState == null)
{
}
else {
savedInstanceState.getString("text");
TextView myText = (TextView) view.findViewById(R.id.textView);
myText.setText(data);
}
return view;
}
So why did it change from return inflater.inflate to View view = inflater.inflate
Sorry for dumb question, but I can't find this answer on Google.
It's basically the same,
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
has to return a View.
And
inflater.inflate(R.layout.fragment_b, container, false)
returns a View as well.
In the second example he assigns the return value of inflater.inflate() to a View object, as he needs to loads some data and set it on this View object... then he returns it.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.lstindicators, container,
false);
And return your view thats all! :D
jejeje... and if you want to change your current fragment for another chechk this:
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit();