I have to call a fragment class from baseAdapter class. I have created a method in main fragment class like this
public void switchContent(Fragment fragment) {
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new Reply_ozoneFragment()).commit();
}
and calling this method in adapter class like this.
holder.bt_reply.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
fragment.switchContent();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.frame_container, new Reply_ozoneFragment());
fragmentTransaction.commit();
/*Intent reintent = new Intent(context,Reply_ozoneFragment.class);
//reintent.putExtra("userid", S);
context.startActivity(reintent);
*/
}
});
but this is not working properly and my app crashes every time. Please tell me what mistake I am doing here.
remove fragment.switchContent(); from your adapter button onclick() method and change
FragmentManager fm = getFragmentManager();
to
FragmentManager fm = getActivity().getSupportFragmentManager()
Related
I am working on an android app changing a listview to a recycler view.
Problem
Using an example project, I handle my clicks to the recylcer in the adapter view.
However, from this adapter, I am having a very had time calling a new fragment.
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// android.app.FragmentManager fragmentManager = getFragmentManager();
// FragmentManager fragmentManager = ((Activity) context).getFragmentManager();
// FragmentManager manager = this.getSupportFragmentManager();
// FragmentManager manager= getContext().getFragmentManager();
// FragmentManager fragmentManager = newsAdapter().getActivity().getFragmentManager();
// getActivity().getFragmentManager();
Log.i("RecyclerViewItemClic", String.valueOf(getLayoutPosition()));
Log.i("RecyclerViewItemClic", personName.getText().toString());
String idnum = String.valueOf(personName.getText());
Log.i("RecyclerViewItemClic", idnum);
Log.i("RecyclerViewItemClic", personAge.getText().toString());
if (personName.getText().toString() .equals("1")) {
/*
fragmentManager.beginTransaction()
.replace(R.id.content_frame
,new numNumbersFrag())
.commit();
*/
((FragmentActivity) context).getFragmentManager().beginTransaction()
.replace(R.id.content_frame, new numNumbersFrag())
.commit();
}
}
The above is the code I attempted resulting in the error
Attempt to invoke virtual method 'android.app.FragmentManager
android.support.v4.app.FragmentActivity.getFragmentManager()' on a
null object
I also get the same error if I try to use getSupportFragmentManager(), or I import fragment instead of the version4.
Any help would be appreciated.
Thank you
change
((FragmentActivity) context).getFragmentManager().beginTransaction()
.replace(R.id.content_frame, new numNumbersFrag())
.commit();
to
((FragmentActivity) view.getContext()).getFragmentManager().beginTransaction()
.replace(R.id.content_frame, new numNumbersFrag())
.commit();
because, in
public void onClick(View view)
view.getContext() will return context which will not be null.
Thanks,
This may also help u :
#Override
public void onClick(View v) {
View view = v.getRootView();
Activity activity = (Activity) view.getContext();
activity.getFragmentManager().beginTransaction().replace(R.id.FrameLayout, fragment).commit();
}
Try this on your Adapter class.
((FragmentActivity)view.getContext()).getSupportFragmentManager()
.beginTransaction()
.replace(R.id.frame_container, new YourFragment()).addToBackStack(null)
.commit();
I am trying to attach a Fragment to my MainActivity programmatically by using the following code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MainFragment fragment = new MainFragment();
fragmentTransaction.add(R.id.activity_main,fragment);
fragmentTransaction.commit();
}
it gives me an error: cannot resolve add method
The application can be found here
Replace your code:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MainFragment fragment = new MainFragment();
fragmentTransaction.add(R.id.activity_main,fragment);
fragmentTransaction.commit();
By this:
Fragment fragment = new MainFragment();
getSupportFragmentManager.begintransaction.replace(R.id.container,fragment).commit;
first of all your take framelayout in activity_main.after that
fragmentTransaction.add(R.id.framelayout_id,fragment);
than its working fine
Make a function like that
public void openFragment(Fragment fragment) {
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.fragmentContainer, fragment).commit();
}
and call this in your onCreate Method
openFragment(new yourFragment());
In your beginTransaction().replace method you have to pass the view from which you want to replace your fragment with
i want to know, how to call a fragment from dialog box click action in android. The dialog box is in another activity.
below is the dialog box section code
public static void Bookingconfirm(final Context _context, String title,String strMessage)
{
final Dialog dialog1 = new Dialog(_context);
Log.e("Point","1");
dialog1.setContentView(R.layout.booking_success);
dialog1.setCanceledOnTouchOutside(true);
dialog1.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog1.findViewById(R.id.d_location);
LinearLayout btnok = (LinearLayout) dialog1.findViewById(R.id.booking_ok);
btnok.setOnClickListener(new OnClickListener()
{
#SuppressLint("NewApi") #Override
public void onClick(View arg0)
{
Log.e("Point","2");
//i wanted the click to fragment here
// parent class of fragment Home.java and fragment is booking.java
}
});
dialog1.show();
}
Fragment fragment= booking.newInstance();
Home mainActivity = (Home) context;
FragmentManager manager = mainActivity.getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.container, fragment, "Frag");
transaction.commit();
Try the above code from your dialog click Listener ...Hope this helps
try the below code:
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new booking());
fragmentTransaction.commit();
Haroon and pradeep answer wont work.
you can pass FragmentManager to your dialog class like this:
import android.support.v4.app.FragmentManager
class TestDialog(val fragmentManager:FragmentManager) {
fun show()
{
showFragmentButton.setOnClickListener{
val fragmentTransaction = fragmentManager.beginTransaction()
val fragment = PurchaseDetail()
fragmentTransaction.replace(R.id.container, fragment, "cashier_fragment")
fragmentTransaction.addToBackStack(null)
fragmentTransaction.commit()
}
}
}
and in your fragment:
TestDialog(getFragmentManager()).show()
I become curious about the difference between these two classes.
My code is..
main_button.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Log.d(MAINBOTTOM_FRAGMENT_TAG,"main button clicked!");
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fblogin_Fragment = new FbLoginFragment();
fragmentTransaction.replace(R.id.frag_container, fblogin_Fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
setting_button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Log.d(MAINBOTTOM_FRAGMENT_TAG,"setting button clicked!");
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(
setting_Fragment = new SettingFragment();
fragmentTransaction.replace(R.id.frag_container,setting_Fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
The project runs fine, nothing extraordinary. However, I'm a little bit confused which one to use it.
OnClickListener or View.OnClickListener?
thanks.
If you're imported View.OnClickListener, both will refer to the same class and will work identically.
View.OnClickListener is just often used to distinguish from DialogInterface.OnClickListener.
I've created a fragment, Fragment01, that extends MapFragment. Problem is that because it doesn't extend Fragment anymore, now the onSelectFragment part of my MainActivity doesn't work anymore. What do I need to change in my MainActivity to get it to work like before? The error I'm getting is on the line newFragment = new Fragment01(); where it says Type mismatch: cannot convert from Fragment01 to Fragment. Being a noob, I simply tried with newMapFragment = new Fragment01(); but that didn't do it.
MainActivity
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
StartFragment startfragment = new StartFragment();
transaction.add(R.id.fragment_placeholder, startfragment);
transaction.commit();
}
public void onSelectFragment(View view){
Fragment newFragment;
if (view == findViewById(R.id.btnstartfragment)) {
newFragment = new StartFragment();
} else if (view == findViewById(R.id.btnfragment01)) {
newFragment = new Fragment01();
} else if (view == findViewById(R.id.btnfragment02)) {
newFragment = new Fragment02();
} else {
newFragment = new StartFragment();
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_placeholder, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
It may be error with different Fragment hierarchies: from support library and platform.
Check your imports and hierarchy of MapFragment.
These are two different types:
android.support.v4.app.Fragment
android.app.Fragment