After scanning all the related questions regarding multiple activities under one tab, I found out that they all refer to using ActivityGroup which is deprecated.
Basically I have 2 tabs, the first one contains a main activity with several buttons and when button is clicked 1 for instance a new activity should open under the same tab, clicking back should bring me to the main view under the first tab. The second tab contains only one activity.
Tried inflating a new view for the sub-activities:
FrameLayout frameLayout = tabHost.getTabContentView();
frameLayout.removeAllViews();
View view = getLayoutInflater().inflate(layoutID, tabHost, false);
frameLayout.addView(view);
But I only get the view, I cannot handle it like an activity; meaning adding buttons or listeners, it's only a view.
Google suggests using fragments instead, but I am having the hard time implementing this. Maybe replacing the activities with fragments could be the solution.
I could really use a good tab\fragments example right about now...
Any suggestions? Thanks in advance.
Yes, the solution to your problem is using Android Fragments.
I understand that you have trouble figuring out what to do. It's everybody's difficulty actually. However, with enough patience to google out things, you will realize it's actually not that difficult.
What I will be telling is basically some tips on how to change your code to fragments. From your current code, there's actually a major change you need to do but it's worth it.
What you are doing is actually you're making a dynamic UI. You have chunks of fragments which eventually can have different listeners which you should later on defined. Here's a sample demo you can actually wok on.
If you want a detailed tutorial, I followed this one. And, yes, it works!
Have fun learning Android and don't forget Android is directly linked to Google. And you can easily google things and ask specific questions here should that question was never asked before.
Related
I'm a new starter of Android. I'm developing my app only by google searching. I learned much things but I couldn't get an answer to this problem.
To use BottomNavigationView, I created two fragment java files and two UI xml files and wrote codes. Until this was good.
private FragmentInside Fragment_Inside = new FragmentInside();
private FragmentOutside Fragment_Outside = new FragmentOutside();
...
transaction.replace(R.id.frameLayout, Fragment_Inside).commitAllowingStateLoss();
Bottomnavview works like this. So no fragments in mainxml, no ID, no tag. I can't use findfragmentbyid.
What I want to do is, if I touch togglebutton2 in fragmentB when togglebutton1 in fragmentA is checked, to make togglebutton1 unchecked. To do this I should be able to get to fragment's resources. but I learned if fragment was made like 'new FragmentA();' I can't.
I applied many solutions in my way with google searching, but always button wasn't unchecked.
I want to learn how to use bottomView and control fragments' view both.
It's the first time I wrote a question. Thank you. And please help me.
Demo1
Demo2
when users click the button to answer the questions for the TextView on the top, the TextView will change the questions depends on the user's answer.
(I think this part won't be hard for me)
But what should I do after user click the button, the buttons will change as well, please see the demo picture.
are there any skills I should study first? like framelayout?
Thank you for your answers.
It really depends on specific needs, but generally speaking, google guidelines are toward the use of fragments.
I'm developing Android apps since just few months and I'm using the single activity/multiple fragments approach. You have to learn how to use fragments, e.g. their lifecycle compared to the attached activity or how to handle events among fragments (EventBus can help you for such task).
You can find more discussions about this approach here
Cheers.
you can create a N fragement with your nuber Button and you set your buttons in this fragement (Demo 1)
but in click a one button you can replace a Specific Fragment (Demo 2) in your framelayout be placed in your Activity
I'm looking for the the best way to reproduce, in an Android app, the behavior of the iPhone UiNavigationController within an UITabBarController.
I'm working on this Android app where I have a TabActivity and 4 tabs. I've already gone through a lot of posts regarding the use of activities and tabs and how it's not a good idea to use activities for everything, which seems fair enough. I decided to use one Activity on each tab anyway, since it makes sense in my application.
However, in one of those activities I have a deep navigation tree with more than one branch and up to 12 different views the user can go through.
The problem is: Android controls the navigation through activities inside an app, if you click the back button it will go to the previous one, but if I'm navigating through views, using one Activity, and I click back, it just finishes it. So how can I have a smooth navigation behavior between views in an Activity?
I had implemented this using a TabActivity with FragmentActivity as each tab. Utilizing Fragments API you can organize the code just like you would be using 12 different activities, still using only 1 for each tab in fact. Fragment's framework will handle back key press for you to show previous fragment instead of closing the entire activity.
There are some problems with such approach, for example, there's no MapFragment, but the workarounds can be found here on SOF.
You will need Android Support Package if your minimum SDK version is lower than 3.0.
Well I know very little about UiNavigationViewController, but I guess you want something to navigate between different Views. As you are using TabActivity, every tab should load into a separate Activity.
But since you want to branch it out, using that many Activities is not a perfect solution, neither the ActivityGroup too. The better solution, as per my opinion(I have run into similar problem once) is to have the main or root tabs loads into separate Activity, but for their branches, use the ViewFlipper, which flips the Views. So the whole Layout(Subclass of View) can be flipped.
You may run into some problem while flipping more than two Views (as what people say, though I never had any problem). So in that case you can use layout.setVisibility(View.GONE) to hide the layout and just change it with View.VISIBLE for next view.
And about the concerns of back button, you need to store the last used View or Activity into a variable, and in the override of onBackPressed(), just need to call them.
There might be better solution than this, not that I can remember, but yeah it's the easiest solution I can come up with.
I am making my first android application with the ActionBarSherlock.
The application will always have an action bar consisting of 3 tabs (first tab selected by default).
The app could be extended for use with a tablet.
I have been searching the web, and following the android development guides, however I am finding a few things confusing.
The first tab screen will be a list view with a list of items, onitemselected should send the user to a screen which features more details about that item.
When should I use a fragment? Should each tab be a fragment?
Or, should each tab call a new activity, which consists of fragments?
And, if using fragments, should I place them in different classes, or embed them within an activity??
Appreciate any help, thanks.
you should probably read these two links first.
http://android-developers.blogspot.com/2011/09/preparing-for-handsets.html
http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html
If you plan to make an app that work on both phone and tablet. It is a good idea to use a fragment, and then use a shell activity to wrap that fragment.
My experience with Fragments is mostly on ViewPager, so I am not entirely sure if it applies here.
In Android, you should use Fragments as much as possible. As a general rule of thumb, imagine you are translating the UI from phones to tablets, elements that can stay together in the same configuration should be a Fragment.
There is a Fragment subclass called ListFragment, so you might want to look into that for your first tab. (ListFragment is for Fragment like ListActivity is for Activity)
There is also a tutorial I found to deal with fragments. Did not really look into it but I hope it helps.
http://android.codeandmagic.org/2011/07/android-tabs-with-fragments/
As for ActionBar / ActionBarSherlock, I have absolutely no experience withit so someone might want to add to that.
I am trying to build an app in which I want to go to a new page through swipe-say. I have about 10 different pages and I want to go to these pages by swiping. Can I implement these pages as separate activities? Can I swipe from one activity to another? I strongly like to implement these pages in seperate xml files rather than creating everything in a single xml page. But as far as I have searched, there are no proper tutorials or blogs giving an appropriate example of a program implementing swipe. If possible, provide some working codes.
Thanks in advance.
It seems that a FragmentPager is what you want.
Examples and code on the linked page.
If you build for earlier versions of Android, the necessary code is included in the Android Support Package.
In Android, swipe is not identified as a particular gesture.
You can use a OnTouchListener registered on your view, and animate your view if the gesture exeeds a certain threshold.
You can then launch a new activity, or in a simpler way, use a viewFlipper.
Please try the attached link View flow.
I feel it will fit your needs perfectly. You can even create different xmls and inflate them into different view objects and then use the "view flow" to switch between them.
The "view flow" is perfect for handling both simple & complicated views. The flow indicator on the top of the screen is optional.
I hope it helps..