I would suggest you save a reference to the fragment manager you got as a variable in another situation, such as when you first started, and use that instead. Takes a little memory, but the context you are in will not effect how you get your manager then.
Invoking Fragment from a popup android
-
15-10-2022 - |
Question
In my main activity layout,I have an image button.
On clicking it,a popup pops up.The layout of the popup is as follows.
test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100px"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/black">
<Button
android:padding="5dp"
android:id="@+id/b_help"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Help"
android:textColor="@android:color/white"
android:background="@android:color/black"/>
<Button
android:padding="5dp"
android:id="@+id/b_pref"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Preferences"
android:textColor="@android:color/white"
android:background="@android:color/black"/>
<Button
android:padding="5dp"
android:id="@+id/b_about"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="About Us"
android:textColor="@android:color/white"
android:background="@android:color/black"/>
</LinearLayout>
The popup is displayed by the following code from MainActivity.java
btnOpenPopup = (ImageButton)findViewById(R.id.menu_button);
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
popupWindow = new PopupWindow(getBaseContext());
popupWindow.showAsDropDown(btnOpenPopup, 50, -30);
}});
The popup window class is as follows PopupWindow.java
public class PopupWindow extends android.widget.PopupWindow implements View.OnClickListener{
Context ctx;
View popupView;
Button help,pref,about;
public PopupWindow(Context context)
{
super(context);
ctx = context;
popupView = LayoutInflater.from(context).inflate(R.layout.test, null);
setContentView(popupView);
setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
// Closes the popup window when touch outside of it - when looses focus
setOutsideTouchable(true);
setFocusable(true);
// Removes default black background
setBackgroundDrawable(new BitmapDrawable());
pref = (Button)popupView.findViewById(R.id.b_pref);
pref.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.b_pref:break;
case R.id.b_help:break;
case R.id.b_about:break;
}
}
}
Now on clicking the preferences button in the popup,i need to display the preference fragment.
I tried this ,for setOnClickListener for the preference button,but it is not working as the popupwindow class cannot extend fragmentactivity and getFragmentManager() is not happening.
FragmentManager mFragmentManager = getFragmentManager();
FragmentTransaction mFragmentTransaction = mFragmentManager
.beginTransaction();
Prefs mPrefsFragment = new Prefs();
//this line..
setContentView(R.layout.activity_prefs);
mFragmentTransaction.replace(android.R.id.content, mPrefsFragment);
mFragmentTransaction.addToBackStack(null);
setContentView(R.layout.activity_main);
mFragmentTransaction.commit();
How can i invoke the preffragment using the button from popup?? Any help will be much appreciated.Cause I am badly stuck at this.
No correct solution
OTHER TIPS
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow