Accessing the same fragment from a button and menu item JAVA - java

I have two questions and your help is really appreciated
First:
is there a way to make a button in the main activity and a menu item open the same fragment from back stack without creating a new one.
Second:
how can I retain what is typed in editText in the fragment without any buttons clicked, should I use outState.putString() in the onSaveInstantState() method or somewhere else and then do I check if it existed should I do it in the onCreate() or onCreateView()?
thank you!

First: yes, you can, just use similar code in button onClick and onOptionsItemSelected
pop the fragment by name example
Second: Save fragment state example

Related

How to display text in a new activity when two radio buttons from two different activities are clicked?

I am using Java and Android Studio. I have one activity with a few options to select from, and once one of them is selected, they click the button that takes them to the next activity. They do the same thing again except this time when the user clicks the button it takes them to a new activity where it displays the results based on the two radio buttons clicked previously. How can I do this?
You can send some variable through intent and onCreate() method of the second activity, you can use that variable to differentiate your functionality.
This video demonstrates the functionality of the radio button: https://youtu.be/zJG9Prn3vZ0
In this video you can know how to pass arguments to another activity: https://youtu.be/OMCctaE66b8
Hope that helps you to solve your problem.
You cannot declare a method inside another method. Put a closing brace after onCreate.
When you are opening a new activity, you are doing it with an Intent. Put the values you want to send (bool clicked) the new activity inside the intent

Switch between fragments from inside one of the fragmens

Two days ago i asked the following question:
Change the fragment in a framelayout from within another fragment of said framelayout
A fellow user Krish, really helped me out in finding out what was wrong with the way i thought, but i am stil not sure how to actually get what i want done.
I want to be able to switch between three fragments in one FrameLayout.
- the first of the three is loaded at the start of the parent fragment and when back is pressed at the second fragment
- the second must be replacing the first at the click of an item in the listview of the first fragment, and when the back button is pressed from the third fragment
-the third must be loaded when a button is pressed in the second layout
I've tried achieving this by calling the following line whenever the fragment must be changed. A1_frame is the FrameLayout of the parent Fragment/Layout and A1_B0_C2 is the fragment i am replacing
getChildFragmentManager().beginTransaction().replace(R.id.A1_Frame, new A1_B0_C2()).addToBackStack(null).commit();
From what i understand the problem with my solution is that it isn't possible to replace a fragment in the FrameLayout of a parent Fragment/Layout, but if it would work, it would solve my solution. thats why i chose to put it in here.
I hope someone is able to tell me what would work!
getChildFragmentManager() returns the Fragment manager of the Fragment it's being called from, in this case whatever Fragment is in A1_Frame
The method you're looking for is getFragmentManager(), which returns the Fragment Manager of the Activity/Fragment that the Fragment is a part of. I.e. MainActivity, or whatever is creating your first fragment.

Restart/Load another Fragment | Android

I tried implement tab view in my app. This is the code that I used
http://developer.android.com/training/implementing-navigation/lateral.html
I created my fragment and I can go change between them. In the main activity I have menu the default menu (three points up right corner).
My question is how can I make that when I click on button in the menu one tab will be update/refresh/become another framgent, The name and the fragment. (I override onOptionsItemSelected..)
Thanks
If it help for someone. The solution is to use FragmentStatePagerAdapter and not FragmentPagerAdapter.
and manipulate the getItem function.
If I understand you correctly you want to use a menu item to switch to a specific tab, right?
To change to a tab grammatically you can use setSelectedNavigationItem like this:
ActionBar actionBar = getActionBar();
actionBar.setSelectedNavigationItem(0);
//this will put the screen to the first tab (tab at index 0).
If you want to do this with a menu item just put it in onMenuItemSelected.
Let me know if you need me to explain in more detail.
=================EDIT=====================
ah i think i understand, so replace the current tab fragment with another one in its place?
I think you can do that by:
actionBar.removeTabAt(position); //position is the current tab position.
actionBar.addTab(tab, position); //insert a new tab at that position
if its not selected after you insert it, try this:
actionBar.addTab(tab, position, setSelected)
i did not test this, so let me know if it does not work.

get a button within a fragment from the activity

How would I do this? Or is it even possible get a button within a fragment from the activity?
I've tried many things e.g. trying to get the view or container of the fragment but no luck.
I am doing this as i have 27 fragments in an activity which all have a same button. Instead of doing a button listener on each fragment, i want to reduce my code greatly but just doing a loop in my activity going through each fragment and setting there button to activate the button listener (in the activity) if pressed.
Once i know how to get a button and set it to the button listener, I can easily do the loop bit my self.
Just finishing my first year for computer science and have been coding in java for a couple of years now.
Thanks in advance!
Don't loop or get reference to the button.
Simply make your activity implement OnClickListener and set it as click listeners for the buttons in all your fragments ..
In the fragment
yourBtn.setOnClickListener(((YourActivityType) getActivity()));
another solution without the implements keyword
is to have a public method in your activity, for example, setButtonAction(Button btn)
public void setButtonAction(Button btn) {
btn.setOnClickListener(myClickListener);
}
and declare myClickListener as variable in youractivity
Same idea without having your activity to implement clickLitener

Android: Method in Fragment Lifecycle

I'm looking for a method in the Fragment Lifecycle, but I'm not sure which one.
Here's my situation: I've got a Fragment inside a ViewPager. The Fragment displays a List with some information. I fill the list in the Fragment's onCreateView(). When the user opens a different Activity (settings in this case) and changes some settings, the information that the List in the Fragment has to show, changes. When the user returns to the Fragment using the Back-button, the onCreateView() isn't re-called, so the information in the List isn't updated.
My question is: The onCreateView()-method isn't called when the user returns to the fragment form a different Activity, but which method is called here? I need to know this because then I can fill the List in that method.
Thanks in advance!
Important and non-obvious point it that Fragment's onCreateView() being called not only in case you selected Tab with this Fragment. So don't rely on onCreateView() of Fragment when using ViewPager(). When You select the Tab, Android creates sible views (caches them) or makes something similar.
You should call your update method when user selects proper Tab in ViewPager (don't remember exactly, but hope it helps).
onResume() is the simple answer, called when user comes back. for more details refer lifecycle here FragmentLifecycle

Categories