Passing arguments in Fragment - android

I' m trying to improve my android app. I have main app menu (with possible actions) in a GridView 3x3. And I decided to add ViewPager to separate my grid. Before I've added ViewPager, I had only one Activity with GridView. In my onCreate method, I calculated the window height with DisplayMetrics to understand, what height should I use for my GridView items.
Now I'm trying to use ViewPager with Fragments. I have my Activity with ViewPager, and 2 Fragments. Each Fragment has the same layout (GridView in LinearLayout). I'm trying to pass screen height into Fragment in this way:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_grid_layout);
pageHeight = getHeight();
Log.d("HEIGHT", "Page height: "+pageHeight);
ViewPager viewPager;
viewPager = findViewById(R.id.main_menu_viewpager);
src = new ArrayList<MainPageTableFragment>();
/*Определяем количество страниц меню*/
int pagesCount = 2;
for (int i = 0; i < pagesCount; i++) {
src.add(MainPageTableFragment.newInstance(i, pageHeight));
}
FragmentPagerAdapter adapter = new ScreenSlidePagerAdapter(getSupportFragmentManager(), src);
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabDots);
tabLayout.setupWithViewPager(viewPager, true);
}
So, each launch of onCreate method should recreate Fragments.
And in Fragment, I get my height in this way:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.activity_main_grid_fragment, container, false);
gridView = rootView.findViewById(R.id.main_menu_grid);
//PROBLEM IS HERE...
height = this.getArguments().getInt("containerHeight");
pageNum = this.getArguments().getInt("pageNumber");
populateGridItems(); //method to load items in gridview
return rootView;
}
The problem is: when I rotate my device, all methods are called, but
height = this.getArguments().getInt("containerHeight");
uses old value. For instance, in first launch (vertical screen orientation), it is 690. When I rotate my device, in onCreate I calculate new height (382), but my Fragment takes old value (690). I tried to call getArguments() in several places (onAttach, onCreate, onActivityCreated), but didn't help.
Can anybody explain me, where is the problem, and what should I do?
Thank you!
UPD: Code of my MainPageTableFragment
public static MainPageTableFragment newInstance(int pageNumber, int height) {
Bundle args = new Bundle();
args.putInt("pageNumber", pageNumber);
args.putInt("containerHeight", height);
MainPageTableFragment fragment = new MainPageTableFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.activity_main_grid_fragment, container, false);
gridView = rootView.findViewById(R.id.main_menu_grid);
height = getArguments().getInt("containerHeight");
Log.d("HEIGHT", "Fragment height in onCreateView: "+height);
pageNum = getArguments().getInt("pageNumber");
populateGridItems();
return rootView;
}
private void populateGridItems() {
/*adding items in GridView*/
gridView.setAdapter(new MenuGridAdapter(this.getContext(), items, height));
}
}

I'm not sure but i think it's because of supportFragmentManager and pagerAdapter.
First time in instantiateItem it creates a name for fragment and in destroyItem it's just detach fragment.
Then after rotating in instantiateItem it founds fragment by name and uses old instance instead of new. I think for optimization and so on.
As a solution, you can remove all fragments from supportFragmentManager or override instantiateItem/destroyItem to remove frgament as you need.

I found another way to do this. In my Activity, I've created public method to calculate height, and in onCreateView in my Fragment, I call this method. It works. But for me, it's unknown problem: why recreation of the fragment took values of arguments from the old bundle.

Related

Different Activity each tab fragment

I have 3 tabs, and I'm trying to show a different activity and layout each tab.
I am using the tabs as my app navigation, so when a has changed, the whole layout and activity need to be changed.
Actually only the layout is changing.
Since the MainActivity.java + layout has the PageAdapter in it (the PageAdapter is loading the tab's content),
I have moved my first page activity from the MainActivity.java to a new java activity called showUpdates.java. Now I'm trying to access this activity as my Tab #1 content.
If you still didn't understand what I'm asking for, The following will make it clear:
My 3 tabs are:
Java: TabFragment1.java, XML: activity_show_updates.xml
Java: TabFragment2.java, XML: tab_fragment_2.xml
Java: TabFragment3.java, XML: tab_fragment_3.xml
The TabFragment1.java:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**
* Was trying:
* Intent intent = new Intent(...)
* startActivity(intent);
*/
return inflater.inflate(R.layout.activity_show_updates, container, false);
}
As you can see, it's moving to a new layout, activity_show_updates.xml.
What I'm trying to do is - using my showUpdates.java as the Activity of the tab_fragment_1.
I've been trying to use Intent but my app crashed when I created it at TabFragement1.java.
Anyone knows how I can make showUpdates.java as the activity of this layout?
You cannot nest activities. There are fragments for that. Just create a different fragment for each tab and use an Activity to hold all of them. If you need it, you can also create child fragments (although the support is not so good and you might have some problems, but nothing really hard to handle).
If you have viewpager working, you can do the following:
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return NUM_ITEMS;
}
#Override
public Fragment getItem(int position) {
if(position == 0){
//return first fragment
}
else if(position == 1){
//return second fragment
}
...
}
and you can do your onCreate logic in onCreateView:
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View yourFragment = inflater.inflate (Resource.Layout.fragment_name, container, false);
... //do stuff
return yourFragment;
}
All you need to do is define which fragments to output in your adapter

How to display images in Android and change it by swipe right/left?

I want to add an introduction to my Android application, to inform the user about how the app works. This intro will be displayed only, if the preferred settings intro will be false. So in this intro, there will be 3 images and at the end, there will be a page, with some text and two buttons, to enable the user to access the application, by making a login. The change between each image, will be made with a swipe movement, (so right to left +, left to right -). How Can I do ?
This can be done via the use of Fragments and ViewPager and FragmentPagerAdapter. Look at this documentation:
FragmentPagerAdapter: http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html
ViewPager:
http://developer.android.com/reference/android/support/v4/view/ViewPager.html
You can have one fragment that is instantiated based on the id in the ViewPager, and that id will indicate which image to show in your image fragment. So for three images, you instantiate a new fragment that sets the image in the fragment based on the current page in the FragmentPagerAdapter. The second fragment can be one for the login buttons and text you want at the end.
Ex for adapter defined in your FragmentActivity (or AppCompatActivity)
public class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return NUM_ITEMS;
}
#Override
public Fragment getItem(int position) {
if(position < 3)
return ImageFragment.newInstance(position);
else
return new LoginFragment();
}
}
Ex for the image fragment for the various images in your introduction:
public static class ImageFragment extends Fragment{
private int mPosition;
public ImageFragment(){
}
public static ImageFragment newInstance(int pos){
ImageFragment frag = new ImageFragment();
Bundle args = new Bundle();
args.putInt("pos", pos);
frag.setArguments(args);
return frag;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPosition = getArguments().getInt("pos");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_image, container, false);
ImageView backgroundView = (ImageView) v.findViewById(R.id.background_image);
switch(mPosition){
case 0:
//set background view image 1
case 1:
//set background view image 2
default:
//set background view image 3
}
return v;
}
}
I would recommend using a ViewPager. Check out this tutorial from the Developer Guide
http://developer.android.com/training/animation/screen-slide.html
If you want to add functionality to each of these pages instead of having just images then perhaps you can implement a fragmentStatePagerAdapter and then put all the functionality in each fragment. Here is a tutorial to implement one.
http://www.truiton.com/2013/05/android-fragmentstatepageradapter-example/
I think we can do it by using recycler view itself.
Using PagerSnapHelper layout manager in recycler view, we can implement swipe to change images.
recyclerView.setLayoutManager(new LinearLayoutManager(this,
LinearLayoutManager.HORIZONTAL, false));
// add pager behavior
PagerSnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);

Android Fragment from Layout - Retain variable data

I've been searching for hours and tried numerous methods but cannot seem to grasp my head around the idea / figure out how to retain/restore data in a ViewPager Fragment when it is destroyed and then recreated.
Here is what I have -
An activity where I setup the ViewPager and PageAdapter
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_pager);
//Setup pager and adapter
mPager = (ViewPager) findViewById(R.id.viewpager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
My PageAdapter where I setup a fragment with a bundle using .newInstance()
#Override
public Fragment getItem(int position) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
return fragment.newInstance(position);
}
My Fragment that has a layout that includes a TextView that shows the user a question, a picture, and two True/False buttons. New instance is returned back to the Adapter.
public static ScreenSlidePageFragment newInstance(int position) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
Bundle args = new Bundle();
args.putInt("page_position", position + 1);
fragment.setArguments(args);
return fragment;
}
//the fragment is newly created for the first time or recreated when exiting the view
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main, parent, false);
//Handle a question being displayed on each fragment
count = getArguments().getInt("page_position") - 1;
mQuestionText = (TextView)v.findViewById(R.id.questionText);
mQuestionText.setText(bank.get(count).getQuestion());
//change the image depending on correct / incorrect answer
mPhoto = (ImageView)v.findViewById(R.id.imageView);
trueButton = (Button)v.findViewById(R.id.true_button);
falseButton = (Button)v.findViewById(R.id.false_button);
//True Button is pressed
trueButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(bank.get(count).getAnswer()) {
mPhoto.setImageResource(R.drawable.right);
clickable = false;
}
else {
mPhoto.setImageResource(R.drawable.wrong);
clickable = false;
}
trueButton.setClickable(clickable);
falseButton.setClickable(clickable);
}
});
What I cannot figure out for the life of me, is how to retain/save that fact that the user has pressed a button and which picture to display when the fragment is restored. I have tried a number of options using onResume(), getArguments(), onSaveInstanceState(), onActivityCreated() etc but none of them seem to work.
I can fix the problem by keeping all my ViewPager pages alive using setOffscreenPageLimit(total pages) but have read this is a bad idea since it takes up a large amount of memory.
Here is an example that I use that can help you where an ArrayList of urls for pictures is saved for later when the view is recreated, you need to override onSavedInstanceState and save your variables in a bundle, then retrieve the values from the bundle when the view is created again, hope this can help
ArrayList<String> picutersUrl = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState!=null)
{
picutersUrl = savedInstanceState.getStringArrayList("My Pictures");
}
#Override
protected void onSaveInstanceState(Bundle bundle)
{
super.onSaveInstanceState(bundle);
bundle.putStringArrayList("My Pictures", picutersUrl );
}
I too have used PagerAdapter to support few static tab widgets. I think you need to add code to ScreenSlidePagerAdapter. Google webpage at PagerAdapter.
Note and read the details on override method instantiateItem. This is where you populate the UI.
Code example for ScreenSlidePagerAdapterfrom (PagerAdapter subclass), using your posted code:
#Override
public Object instantiateItem(ViewGroup container, int position) {
View v = inflater.inflate(R.layout.fragment_main, parent, false);
...
trueButton = (Button)v.findViewById(R.id.true_button);
}
In reality with Views in this framework, you're responsible on saving the state of UI elements. In my ListView, I am caching and saving up to 100 rows of data and refreshes it in an Adapter, for example. For caching, I created custom class containing the UI data.

OncreateView does not call when Activity get destroyed and re-run again

I'm getting a strange problem with Fragment and ViewPager.
I have 1 ViewPager contains 3 Fragments.
When my app run for the first time. onCreateView on all my fragments get called and ViewPager works perfectly.
Code for initalizing ViewPager
private void initPager() {
mFragmentList.add(new MainSongListFragment());
mFragmentList.add(new MainAlbumArtFragment());
mFragmentList.add(new MainLyricsFragment());
if (mFragmentList == null)
mFragmentList = new ArrayList<Fragment>();
MainPagerAdapter pagerAdapter = new MainPagerAdapter(
getSupportFragmentManager(), mFragmentList);
mMainPager.setAdapter(pagerAdapter);
myListViewInsideFragment= (ListView) findViewById(R.id.my_list);
}
When I press back button and run app again. I get error at the code:
myListViewInsideFragment= (ListView) findViewById(R.id.my_list);
It returns null because the onCreateView method on Fragment does not get called so all views on this fragment is not avaiable.
Code in My Fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.lo_my_list, container,
false);
mListView = (ListView) view
.findViewById(R.id.my_list);
return view;
}
My question is:
Why does not onCreateView method get called when Activity get destroyed and re-run again?.
Thank you.

View Pager in Fragment

Once again, I am so annoyed by the AndroidSDK . Why does this have to be so complicated?
I have a ViewPager that I want to populate with my Fragment. I implemented this on an Activity and it works. But when its in another Fragment the whole thing crashes and the erros messages tells me nothing:
07-01 12:58:20.210: E/AndroidRuntime(10146): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Here is my code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_tabwrapper, container);
List<Fragment> fragments = getFragments();
pageAdapter = new MyPageAdapter(getActivity()
.getSupportFragmentManager(), fragments);
pager = (ViewPager) v.findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter); // <- CRASH!
return v;
}
BTW: The whole thing is based on this tutorial: http://architects.dzone.com/articles/android-tutorial-using
This error occurs beacuse you have to remove all views before set adapter in view pager so get right way..... So check this:
MyPageAdapter pageAdapter ;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_tabwrapper, container);
List<Fragment> fragments = getFragments();
pageAdapter = new MyPageAdapter(getActivity()
.getSupportFragmentManager(), fragments);
pager = (ViewPager) v.findViewById(R.id.viewpager);
pager.removeallviews();
pager.setAdapter(pageAdapter); // <- CRASH!
return v;
}
you should not use the ViewGroup that onCreateView provides as container to put your inflated view:
Change
View v = inflater.inflate(R.layout.fragment_tabwrapper, container);
to
View v = inflater.inflate(R.layout.fragment_tabwrapper, null);
This might shed a little light on the topic: http://developer.android.com/about/versions/android-4.2.html#NestedFragments
IMHO: I don't understand why developers put up with these tools. I find them impossible to work with. No wonder there are so many low-quality apps on the Play store. I'm looking forward to the day this project is done. Won't be working with this OS anytime soon again ...

Categories

Resources