Fragment android studio - android

I want to make 2 fragment, if I press the question then will replace the fragment question, and if the result of the fragment results. but of these codes are replaced only fp.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tersumbat);
strings = getResources().getStringArray(R.array.Tersumbat);
hasilstrings = getResources().getStringArray(R.array.HasilTersumbat);
FragmentManager fm = getSupportFragmentManager();
Tersumbat.FragmentPertanyaan fp = new Tersumbat.FragmentPertanyaan();
Tersumbat.FragmentPertanyaan fh = new Tersumbat.Fragmenthasil();
fp.setListener(this);
Bundle bundle = new Bundle();
bundle.putString("pertanyaan", strings[0]);
bundle.putString("hasil", strings[0]);
bundle.putInt("posisi", 0);
fp.setArguments(bundle);
fh.setArguments(bundle);
fm.beginTransaction().replace(R.id.content_frame, fp).commit();
fm.beginTransaction().replace(R.id.content_frame, fh).commit();
}

Try this code
fragname fragname = new fragname();
Bundle bundle = new Bundle();
bundle.putCharSequence(bundle_string, yourstring);
fragname.setArguments(bundle);
FragmentManager manager = getActivity().getSupportFragmentManager();
manager.beginTransaction().replace(R.id.fragmentlayout, fragname, fragname.getTag()).commit();
Note:
if you're in a Activity and you're calling a new fragment, take out the IgetActivity()., its only necessary whet you call a fragment from another fragment

Related

How to send Data from Fragment to other Fragment inside FirebaseAdapter, using model.class to getPid

i have being trying to solve this problem for while thought, i'm trying to pass the data from Fragment to other Fragment, the model.getPid suggesting me to be static!, and after running the app and clicking on the image then the app crachs!
thanks in advance!
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ProductsDetailsFragment fragment = new ProductsDetailsFragment();
Bundle b1 = new Bundle();
b1.putString("pid", Products.getPid());
fragment.setArguments(b1);
FragmentManager fragmentManager =
getActivity().getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.nav_host_fragment, fragment)
.addToBackStack(null)
.commit();
this is from the second Fragment
Bundle bundle = this.getArguments();
if (bundle != null) {
productID= bundle.getString("pid");
getProductDetails(productID);
}
in the FirebaseAdapter when i pass the ProductTD, it returns null
private void getProductDetails(String productID) {
DatabaseReference productRef = FirebaseDatabase.getInstance().getReference().child("Products");
productRef.child(productID).addValueEventListener(new ValueEventListener() {
#Override
this is from Logcat
java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
here is your error Intent intent = new Intent(getActivity(), ProductsDetailsFragment.class);
intent.putExtra("pid", model.getPid());
startActivity(intent)
remove this you can use intent for fragment

Transfer data from one fragment to another fragment

I am developing an app in which I have used bottom navigation bar so for that I had to use fragments. In my fragments, I implemented Recycler view. So My question is when I click on an item of recycler view, how can I navigate to another fragment (which is in a different item of the bottom navigation bar) and how can I transfer the data between two fragments. Please Help.
during onClick() pass data through bundle
like that
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
Bundle bundleobj = new Bundle();
bundleobj.putCharSequence("key", data);
Fragment2 fragobj = new Fragment2();
fragobj.setArguments(bundleobj);
mFragmentTransaction.addToBackStack(null);
mFragmentTransaction.replace(R.id.containerView, fragobj).commit();
In Fragment2 class:
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle =getArguments();
if(null!=bundle) {
myData=bundle.getCharSequence("key");
}
}
In first Fragment..
Fragment fragment = new EditExamFragment();
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
Bundle bundle = new Bundle();
bundle.putString("branch_id", mDataset.get(position).getiBranchId());
bundle.putString("exam_id",mDataset.get(position).getiExamId());
fragment.setArguments(bundle);
In Second Fragment for fetching values try below code;
String branch_id, exam_id ;
final Bundle bundle = this.getArguments();
if (bundle != null) {
branch_id = bundle.getString("branch_id");
exam_id = bundle.getString("exam_id");
}
Add below code after event listener in fragment1
SecondFragmentName secondFragmentName = new SecondFragmentName();
Bundle args = new Bundle();
args.putString("key", "value");
secondFragmentName.setArguments(args);
getFragmentManager().beginTransaction().replace(R.id.content_frame, secondFragmentName).addToBackStack("Some string").commit();
To get value in Fragment2
String message = getArguments().getString("key");

Android Bundle Nullpointerexception

I have two fragments and I am trying to pass an integer between them. In the first one, in Fragment_1.java I have this in onCreate:
pontszam = 12;
Fragment fragment = new Fragment_2();
Bundle bundle = new Bundle();
bundle.putInt("pont", pontszam);
fragment.setArguments(bundle);
Then in the second fragment, in Fragment_2.java I have this in onCreate:
Bundle bundle = this.getArguments();
int myInt = bundle.getInt("pont");
TextView pontjelzo = (TextView)rootView.findViewById(R.id.pontszam_2);
pontjelzo.setText(Integer.toString(myInt));
But I get nullpointer exception on this line:
int myInt = bundle.getInt("pont");
What am I doing wrong?
And this is how I open my second fragment:
button_1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager ();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction ();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left).replace(R.id.content_frame, new Fragment_2());
fragmentTransaction.commit ();
}});
You are replacing your first Fragment with a new Fragment_2 instead of the one which has a Bundle.
Declare your Fragment_2above onCreate (globally), instantiate it:
fragment = new Fragment_2();
And change your replace line:
fragmentTransaction.replace(R.id.content_frame, fragment);

Adding and replacing Fragments , is not working as expected in android

I was trying out android fragments, and i could not understand this; please help
public class MainActivity extends FragmentActivity {
private static final String TAG = "MyActivity";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.fragment_container) !=null) {
if(savedInstanceState!=null){
return ;
}
ArticleFragment first_fragment = new ArticleFragment();
HeadlineFragment second_fragment = new HeadlineFragment();
first_fragment.setArguments(getIntent().getExtras());
second_fragment.setArguments(getIntent().getExtras());
FragmentTransaction manager=getSupportFragmentManager().beginTransaction();
manager.add(R.id.fragment_container, first_fragment,"first");
manager.add(R.id.yo, second_fragment,"second");
manager.commit();
}
}
now i want to remove second_fragment at a button press and replace it with another. So i tried this
if(getSupportFragmentManager().findFragmentByTag("second")!=null) {
Log.d(TAG,"found");
abc newFragment = new abc();
Bundle args = new Bundle();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.remove(getSupportFragmentManager().findFragmentByTag("second"));
transaction.add(R.id.fragment_container,newFragment,"third");
transaction.addToBackStack(null);
transaction.commit();
}
but when i press that button again and again, the if condition passes.
What is wrong here?
Don't Do removing and adding. Simply Replace second fragment with the new one. You will be done.

Cant understand fragment replace

I have trouble understanding in FragmentTransaction.replace(id, fragment, 'string') what does .replace actually
I read somewhere that it will replace the content of view id with fragment but in my
public class MainActivity extends
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.fragment_container) !=null) {
if(savedInstanceState!=null){
return ;
}
ArticleFragment first_fragment = new ArticleFragment();
HeadlineFragment second_fragment = new HeadlineFragment();
first_fragment.setArguments(getIntent().getExtras());
second_fragment.setArguments(getIntent().getExtras());
FragmentTransaction manager=getSupportFragmentManager().beginTransaction();
manager.add(R.id.fragment_container, first_fragment,"first");
manager.add(R.id.fragment_container, second_fragment,"second");
manager.commit();
}
abc newFragment = new abc();
Bundle args = new Bundle();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment,"second");
transaction.addToBackStack(null);
transaction.commit();
}
}
it only replaces the first fragment that i added to Transaction, i.e. first_fragment.
And how do i replace a specific fragment?
The issue seems to be with R.id.fragment_container. You should have a different first argument on the line below in order to replace a fragment that is not your "first" fragment:
manager.add(R.id.fragment_container, second_fragment,"second");

Categories

Resources