I have the following model.
A TabFragment which extends Fragment.
A ListFragment which extends Fragment.
And a DetailFragment which extends Fragment.
TabFragment hosts ListFragment which will launch a DetailFragment.
When I call getActivity().setResult(Activity.RESULT_OK, i); from DetailFragment, onActivityResult is called, but only in TabFragment. I pass from Detail Fragment, one String identifier to TabFragment.
My only problem is that I would like to pass this String from TabFragment back up to ListFragment.
What is the best way to do this?
You can find a fragment you need with FragmentManager's findFragmentById() or findFragmentByTag() where id/tag is one you use whan you add a fragment. After that you can interact with this particular fragment.
Related
i need some help. I'm making an app on android studio and I have a Button initialized in a Fragment. I need to call this button in another Activity and set a click listener on it.
The problem is that when I do this I get the .NullPointerExceptionError and it says that the value of the object is null.
Now i tried to initialize the button in both my fragment and Activity but I keep getting that error and the app crashes.
I need some help to call this button from the Fragment. Maybe my question is similar to some other but it's hours that I'm searching a solution to this problem and I found nothing.
Thanks in advance for all your help
Since the button is part of the fragment, it only fits that any interactions/modifications to the button be defined in the Fragment's scope itself.
There are 2 ways to go about it:
Create the click listener in the Fragment itself and navigate to another activity from there.
Create an interface which your activity implements and the fragment has an instance of that interface. On the button click, call the interface's method. That would give you control in the activity which is hosting the fragment.
MyActivity extends AppCompatActivity implements MyClickListener {
void onMyButtonClick() {
// Navigate to another activity here
}
}
MainFragment extends Fragment {
private MyClickListener listener;
void onAttach(Context context) {
listener = (MyClickListener) context;
// Now call listener.onMyButtonClick() from button's on click listener.
}
}
interface MyClickListener {
void onMyButtonClick();
}
My MainActivity layout includes a bottom navigation bar because I would like to use this as the main navigation components within my application's fragments. However, I have a login page which I do not want the navigation bar to be visible on. Since the fragment is being created from the MainActivity, it inherits the navigation bar and I can't find a way to hide it on the login fragment and show it on subsequent fragments.
For ease:
I have a MainActivity layout with a BottomNavigationBar
All fragments natively inherit the BottomNavigationBar from the MainActivity layout
I want the visibility of the navigation bar to be "GONE" on the user login fragment
I want the visibility of the navigation bar to be "VISIBLE" on the remaining fragments
Is there a way to do this?
The reason I am defining the BottomNavigationBar within the MainActivity is that I initially had a separate navigation bar on each fragment which required separate listeners on each fragment (making the code more extensive than it should be). Additionally, when I implemented the navigation bar as an individual component within each fragment, the item selected animations no longer functioned.
I have tried to use <include layout="#layout/main_activity android:visibility="GONE" android:layout_width="match_parent" android:layout_height="wrap_content"/> from within the login fragment but this has not worked.
I would appreciate any help people have on this matter.
Please feel free to let me know if you would like to see any of my code. I wasn't really sure which parts of my code would be relevant.
I would suggest you to use separate activities for this case - LoginActivity and MainActivity. Since you may at some point add ForgotPasswordFragment and maybe something else. To hide something you don't need in the first place is bad practice IMHO.
In case you don't want to change your approach you can create BaseFragment that will have abstract val showBottomNavBar() and will override it in every fragment.
And then in onViewCreated you'll check that flag and update UI accordingly.
Something like this:
abstract class BaseFragment : Fragment() {
abstract val showBottomNavBar: Boolean
//.. your other stuff
}
class FragmentA : BaseFragment() {
override val showBottomNavBar = false
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if (!showBottomNavBar) {
(requireActivity() as YourAcitivy).hideNavBar() //this is bad
someKindOfBroadcastManager.sendEvent(hideNavBar) // this is somewhat better
}
}
}
But I would still suggest you to decouple login/main logic from single activity
You can access activity from fragment with getActivity() method and cast to your activity. Of course you have to add hideBottomNavigationBar method to your activity and also you have to be sure that your fragments host activity is MyActivity.
MyActivity activity = ((MyActivity)getActivity());
activity.hideBottomNavigationBar();
After that you can define a marker interface that named IFullScreen then in your BaseFragment's onCreateView method you can check if this class is instance of IFullScreen and decide to show or hide bottom navigation bar.
interface IFullScreen { }
class BaseFragment extends Fragment {
#CallSuper
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
MyActivity activity = ((MyActivity)getActivity());
if (this instanceof IFullScreen)
activity.hideBottomNavigationBar();
else
activity.showBottomNavigationBar();
}
}
class FragmentA extends BaseFragment implements IFullScreen {
//Rest of fragment code
}
I creating my own Fragment that I wanna activate from my activity. My mainActivity file is a pure Activity (not a FragmantActivity or anything else). Why I can't use my custom Fragment as a parameter?
if (savedInstanceState == null) {
// Add the fragment on initial activity setup
myFragment = new CustomFragment();
getFragmentManager()
.beginTransaction()
.add(android.R.id.content, myFragment )
.commit();
I am getting a compile error telling me that the 2nd parameter on .add() method must be a Fragment, but 'myFragment' is a CustomFragment that derives Fragment class. So how to activate this fragment from a normal Activity?
Make sure that you're consistent with your Fragment imports. Use either import android.app.Fragment in all classes or import android.support.v4.app.Fragment; in all classes. Don't mix-and-match
FragmentActivity or any child class of FragmentActivity only can hold the fragment from the support library. Hence change your activity's parent class to FragmentActivity, it will work. As AppCompatActivity inherits from FragmentActivity it also will work.
I trying to create a navigation drawer in my apps. I have 3 present options(View,Claims,Report) and of course the 3 activities are extend to fragment. In View, it has a next button which will go to another page(UpdatePage1.java). Does the page need to extend to fragment or just extend to Activity?
I extend the UpdatePage1 to fragment and my app crashed.
LogCat
Unable to find explicit activity class {com.example.project.project/com.example.project.project.UpdatePage1}; have you declared this activity in your AndroidManifest.xml?
Why would this happen? I thought it never need to add fragments to manifest? Please help.
To call Fragment from another Fragment:
SecondFragment secFrag = new SecondFragment();
FragmentTransaction fragTransaction = getFragmentManager().beginTransaction();
fragTransaction.replace(R.id.fragment_layout,secFrag );
fragTransaction.addToBackStack(null);
fragTransaction.commit();
I am trying to cast an activity to a FragmentActivty object so I could get FragmentManager object
public class Main extends ListActivity {
...
public void showTimePickerDialog(View v) {
FragmentActivity myContext=(FragmentActivity) getApplicationContext(); //Here: java.lang.IllegalStateException: Could not execute method of the activity
FragmentManager fragManager = myContext.getFragmentManager();
DialogFragment newFragment = new uSharedUtility.TimePickerFragment();
newFragment.show(fragManager, "timePicker");
}
}
But when doing it I get:
java.lang.IllegalStateException: Could not execute method of the activity
I cant use getFragmentManager directly because my activity extends from ListActivity which is very necessary.
Please suggest me to get around this error, i really need to use both ListAdapter & Date/Time pickers in the same activity.
I cant use getFragmentManager directly because my activity extends from ListActivity which is very necessary.
It isn't necessary. You can have a ListView in any activity.
Since you're already using fragments, consider using a ListFragment with a FragmentActivity.
You can't cast Application Context to Activity, you must need Activity Context.
(FragmentActivity) getApplicationContext() // Not Possible
getContext(): Returns the context the view is currently running in. Usually the currently active Activity.
getApplicationContext(): Returns the context for the entire application (the process all the Activities are running inside of). Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, not just the current Activity.