Restart/Load another Fragment | Android - java

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.

Related

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.

Accessing the same fragment from a button and menu item 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

How I can add buttons to the Action Bar?

I want to add buttons to my action bar. How I can do this?
You'd better start with some search and if could not find any answer then ask your question!
By the way:
create an android resource file and name it something like menu_buttons.xml.
Then inflate it and assign to a View object in onCreate method and use it in your ActionBar like:
View v = LayoutInflater.from(yourContext).inflate(R.layout.menu_buttons, null);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(v);
http://www.androidhive.info/2013/11/android-working-with-action-bar/ Refer this link... It explains everything from scratch and you have everything you need to know like adding menu items on action bar... handling click events and what code to add where.. So pls go through this

Markable ListView with changing Actionbar Actions

Im trying to find out how I can mark things in ListViews with a longClick. After I selected one item ,the Actionbar should change with diffrent options (buttons like delete, add , copy etc) which Im able to execute.I really didnt know how I can find these examples because Its a kind unique I guess. I founded threads where I can Mark an Item which gets a diffrent color and nothing else. How can I achieve this ?
Before selecting :
After long click :
Well you can add an onLongClickListener in your ListView or RecyclerView but for each list_item you will have to specify a checkbox that will become visible only when you longClick the ListView.
Then if you do not want to mess up the actionBar with new menu items you can create a context menu that will do your job.
You can use a ListPopupWindow or you can also use a CardView with a contexual menu as shown in the picture here.

How create Activity with a few ListView?

I have to create Activity with ListView, when I click on item on ListView item and if this item has more items, new List sliding from left side to half of screen and so on...
It should be kind of this
http://a1.mzstatic.com/us/r30/Purple6/v4/d5/92/2a/d5922a96-fccf-508a-52fb-c3a21a8d8d4c/screen480x480.jpeg
Does somebody have some idea?
I don't need code just show me direction, but if you provide some simple example I will be glad to see it:)
Thank you
http://www.vogella.com/tutorials/AndroidListView/article.html
This link has an implementation of how to make a listView. You would have 2 different activites with two listViews. And while implementing the onitemclick method of one listView you will have to set up an intent which will open the second listView activity. Did that gelp?
Even I am new to android so I hope that logic helped. I cannot explain without a code.

Categories