Advantages of using Tabs over Intent - android

I just found out that we can use overridePendingTransition(0,0) to make a very simple transition between Layouts that override the current Android transition.
switch(v.getId()){
case R.id.btnCorner:
Intent i = new Intent(MainActivity.this, Settings.class);
startActivity(i);
overridePendingTransition(0,0);
break;
}
What is the advantage of using tabs over such a simple procedure?
My opinion is that tabs facilitate the transmission of variables between one "window screen" to another. Rather than passing the variable through intents.
Please note that I am not familiar with using Tabs and I hope someone can clarify the idea of using Tabs in an application.

Jonathan Hugh, nice questions really helpful for users , here i am going to give you brief description for both like : Intent take an example of any master-detail form where we want by using click on any ListView item row need to call another activity in this case i recommend you to use Intent because in same flow you require your result to be done, and other side, using tab it will give you more convenience to put your wishlist features in app using separate-separate tabs for all....
Tabs Like:-
private void setTabs()
{
addTab("Tab1", R.drawable.tab1, tab1.class);
addTab("Tab2", R.drawable.tab2, tab2.class);
addTab("Tab3", R.drawable.tab3, tab3.class);
addTab("Tab4", R.drawable.tab4, tab4.class);
}

What do you meen under tabs? There are several ways in Android to implement Tabs pattern. Some of them are better, other are not very good or deprecated. Not all of them are :
facilitate the transmission of variables
For example, using 3 different Activity, will significatelly increase amount of code, increase code connectivity and decrease code quality.

According to my point of view, in tabs, you can organize your work more systemically as compare to Intent.

Related

How to handle multiple Fragments for a single Activity

I have recently started to take a look at developing apps for my android device. What started this interest for me is I was playing around with a few arduinos had the great idea to make them communicate with my phone, as say an interface for whatever values I am measuring on the arduino itself. Now I could take the easy way out and use a public source to accomplish this but there isn't as much to learn that way, and I would like it the way I want it.
Now I suppose the first question I need to ask is, would multiple fragments/single activity be the best way for me to accomplish this? Basically, I want 1 connection to the arduino, pull all the values, but depending on the "tab" I have selected I want certain values displayed certain ways. I decided to make each tab a different fragment and just display the values different ways. Like I said, I am just beginning android development experience so don't have much to base this choice off of.
So being fixated on this multiple fragment idea I have:
Created multiple Fragment.xml files
Defined a class for each separate view
Created a List to display available Fragments
Instantiated and displayed fragment when selected
So essentially my onMenuItemSelect looked like this.
FragmentTransaction FT = getFragmentManager.beginTransaction();
switch(position){
case 1:
FT.replace(R.id.fragment_container, new MyFragment()).commit();
break;
case 2:
FT.replace(R.id.fragment_container, new MySecondFragment()).commit();
break;
}
The above code worked, it did what I wanted it to without any issues. I don't really like this though, because for each and every Fragment I wanted to add I would need to add a new case to the switch. Also this instantiates a new fragment every time, even if one was already created. Is that a problem?
The biggest problem I had with it is that it isn't the easiest to scale. For 2-3 fragments this isn't the worst way to handle it (in my eyes). I want to be able to have as many fragments I want without an individual case for each one in the switch. So what I did was created a fragmentList to hold a single instance of each of my fragment.
List<Fragment> fragmentList;
private void populateFragmentList();{
fragmentList = new ArrayList<Fragment>();
fragmentList.add(new HomeFrag());
fragmentList.add(new BluetoothFragment());
fragmentList.add(new USBFragment());
fragmentList.add(new RCInfoFragment());
fragmentList.add(new ControllerFragment());
fragmentList.add(new FingerCordsFrag());
}
public void onMenuItemSelect(int position, int curPosition){
if(fragmentList.get(position).isAdded()){
getFragmentManager().beginTransaction().show(fragmentList.get(postition))
.hide(fragmentList.get(curPosition)).commit();
}
else
getFragmentManager().beginTransaction().add(R.id.fragment_container, fragmentList.get(position)).show(fragmentList.get(position)).hide(fragmentList.get(curPosition)).commit();
}
And this method also worked. I could get it to display all of my fragments, without have to re-instantiate each fragment each time. I believe this does what I am wanting it to do, it scales fairly well(better than a switch/case IMO). The problem that I have now is that it all goes crazy when I change orientation. Up until now I was only testing portrait mode, I am not able to view any of my fragments when I select them in other orientation. I can start it in either orientation, and it works, but when I change it during run-time, I am only able to see the one fragment I had open when I changed orientation.
Now, each fragments "onCreateView" is being called, it is just that the display isn't being shown. I believe I have it narrowed down to it isn't being attach to the new activity created from the orientation change. Is There anyway I can reattach fragments that are already created to a new activity.
In summary, the questions I have are:
Is this model even a good way for my application?
Is there a decent way to handle Fragments that scales well? Can't
seem to find any examples.
Is using a ''new MyFragment()'' each time I open a different tab a
reasonable way to achieve this?
Is my way of storing my Fragments in a list a reasonable way to
handle them?
How do I reattach a fragment to the new Activity after an
orientation change?
Thank you for your time.
*Had to type all this code on the fly because I, for some reason, couldn't get my C/P'd code to format correctly.
I believe it a good choice to use fragments and start with this example...
You should definitely override some "Adapter" to handle all the transactions more easily...
Check here for the orientation problem...

How to implement a succession of activities within a single tab of a tabhost?

I have implemented a TabHost. In one tab I have Activity1, which calls Activity2 after a button click, which calls Activity3 after a button click, which calls Activity1 after a button click, etc.. No backstack functionality is required, just 1 --> 2 --> 3 --> 1, etc. All three activities have a separate layout file.
Everything works fine, except that after the first transition from 1 --> 2 the activities grab the entire screen and the tabs are invisble forever.
Question: how can I keep these three activities within the confinement of de tab area and the tabs visible? The problem has been recognized here many times before; the solution used to be ActivityGroups, but these are deprecated and Fragments are advised instead. I have seen many examples here, but nothing that could help me.
Can I keep my three activites (Activity1 extends Activity, etc)?
Should I add fragment tags to the layout files?
Do I need to work with transactions?
Should I work with one fragment class or three?
Can you please give me a few hints how I should go about? I woud already be helped if you tell which classes I need to use and of what type they are.
Thanks in advance.
It took me more than half a day, but finally found a solution that works. Unfortunately I am still stuck with deprecated issues (Activity Group and getLocalActivityManager().startActivity(..)).
Again I have a single tab under a TabHost and several activities, all operating within that tab. Navigation from one activity to the next occurs with a buttonclick. Solution:
all Activities operating within the tab need to extend ActivityGroup
All Activity classes need to have a button handler that links to the next activity like this:
public void onBtnClicked(View view) {
Intent intent = new Intent(view.getContext(), NextActivity.class);
replaceContentView("NextActivity", intent);
}
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id, newIntent.
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
this.setContentView(view);
}
By this the tabs remain visible all the time, as desired.
Hope this helps someone.

How to use View Flipper to navigate between two activities

I have two activities and I want to navigate between these two activities with flipview?
view flipper is used only for navigation between views only not for activities.
From android 2.1 onwards there is method
Intent intent = new Intent(Fisrst.this, Second.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_left, R.anim.slide_right);
so use like that. I think it will helpful for you.
Have a look at ViewPager

Android:How to change opened tab dynamically

I have an Android application which has four tabs (I use a main TabActivity with TabHost and TabSpecs).
In one of my sub activity (activity opened in a tab), i need to open a tab not by clicking on the tab title and i don't know how to do this.
For example, i have a button in my activity and when i click on it, it opens a different tab.
For the moment, it is what i do:
Intent intent = new Intent(myActivity.this, myTabActivity.class);
intent.putExtra("ComeFrom", true);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Then in the TabActivity, if i get true reading the "ComeFrom" extra i open the wished tab but the problem is that it kills all the other activies. So, if someone knows a better (cleaner) way to do that trick, please tell me...
Found an easier (I think) answer:
on the TabActivity declare a public, static and self variable and populate it on the onCreate method. F.e.:
public class TheActivity extends TabActivity {
public static TheActivity self;
...
#Override
public void onCreate(Bundle savedInstanceState) {
self=this;
on any Activity running in a tab, when you want to change the one shown on your app. you can do this:
TabHost tabHost = TheActivity.self.getTabHost();
tabHost.setCurrentTab(0);
Worked ok for me, hope serves someone else!
You have to use TabHost's "setCurrentTab(...)" for that. In one of my projects, I created a static method in the main Activity (the one with the TabHost), named "swtichToTab(int tab)". In my subactivites (those inside the tabs) could then just call "MainActivity.switchToTab()" to trigger switching.
It may not be the cleanest method, I'm sure you can achieve this using broadcast intents too.
You can create a BroadcastReceiver and send a broadcast with the index of the tab as extra
You can use views instead of activities for the content of the tabs. This way, the code is simpler and doesn't use as much memory. Plus, you then can use the setCurrentTab(tabIndex) method to easily switch between views.
I have a simple tutorial here. It has a tab activity with a list and map view. When you you click on an item in the list, the activity dynamically goes to the map view (using the setCurrentTab(tabIndex) method). You can easily modify this to have a button switch views.

How to communicate between Android tabs

Im trying to setup some tabs for my android application, but i got stuck.
I cant find a way to communicate between the tabs..
I got 2 tabs.
|Search|Result|
The search tab is simply showing a TextEdit and a "Search" button.
Hitting the search button should make my app change to the result tab and display the result.
i have added the tabs as activities using new intents as
TabHost host=getTabHost();
host.addTab(host.newTabSpec("one")
.setIndicator("Search")
.setContent(new Intent(this, SearchTabActivity.class)));
host.addTab(host.newTabSpec("Result")
.setIndicator("Android")
.setContent(new Intent(this, ResultTabActivity.class)));
Now i cant find a way for SearchTabActivity to display ResultTabActivity and alter its view...
Im looking forward for any tip.
You definitely want to reconsider using Activities as the content of your tabs. The more standard approach is to use one Activity that uses Tabs to only show part of the layout when a particular tab is selected.
The Android documentation has an excellent worked example, check out Hello, TabWidget.
Alternative
If for some reason you do need to use Activities, you can pass information between them by either adding values to the extras bundle within the Intent your using to open each Activity, or by extending the Application class.
By extending the Application class (and implementing it as a Singleton) you get an object that will exist whenever any of your application components exist, providing a centralized place to store and transfer complex object data between application components.

Categories

Resources