aspose file tools*
The moose likes Android and the fly likes How to avoid starting default Activity in Tab Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "How to avoid starting default Activity in Tab" Watch "How to avoid starting default Activity in Tab" New topic
Author

How to avoid starting default Activity in Tab

Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 249

Hello,

In my Tab application I want to launch 3rd Activity as default Activity. For this I am using tabHost.setCurrentTab(2).
It works but starts both 1st and 3rd Activities and brings 3rd activity in front. My 1st Activity makes a call to web services thus it takes a lot of time for 3rd Activity to get visible.
My question is how can I avoid 1st activity ti get loaded when TabActivity is called?

Thanks


Astha - OCPJP 6 (90%)
Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 249

I tried implementing some code for this. I overridden method getTabHost() of TabActivity and created my own TabHost by extending TabHost in my class MyTabHost to get object of TabHost class. Then I overridden method setCurrentTab() of class TabHost in MyTabHost and implemented it in such way, so that super.setCurrentTab() doesn't get called if tab index is 0. I this way default call to this method for first tab can be prevented.
I think this logic should work but don't know what goes wrong and the line 1 gives NullPointerException even when neither tabHost nor requestsSpec is null.
I would be too grateful if anybody can check this code and help me out.


MyTabHost-
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

What is the complete exception stack trace?


Swastik
Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 249

Hi Swastik, This is the stack trace-

04-09 11:11:30.316: E/AndroidRuntime(417): FATAL EXCEPTION: main
04-09 11:11:30.316: E/AndroidRuntime(417): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.devrepublic.kommaaropmetjeaanbieding/com.devrepublic.kommaaropmetjeaanbieding.ui.HomeScreenTabActivity}: java.lang.NullPointerException
04-09 11:11:30.316: E/AndroidRuntime(417): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-09 11:11:30.316: E/AndroidRuntime(417): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-09 11:11:30.316: E/AndroidRuntime(417): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-09 11:11:30.316: E/AndroidRuntime(417): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-09 11:11:30.316: E/AndroidRuntime(417): at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 11:11:30.316: E/AndroidRuntime(417): at android.os.Looper.loop(Looper.java:123)
04-09 11:11:30.316: E/AndroidRuntime(417): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-09 11:11:30.316: E/AndroidRuntime(417): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 11:11:30.316: E/AndroidRuntime(417): at java.lang.reflect.Method.invoke(Method.java:507)
04-09 11:11:30.316: E/AndroidRuntime(417): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-09 11:11:30.316: E/AndroidRuntime(417): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-09 11:11:30.316: E/AndroidRuntime(417): at dalvik.system.NativeStart.main(Native Method)
04-09 11:11:30.316: E/AndroidRuntime(417): Caused by: java.lang.NullPointerException
04-09 11:11:30.316: E/AndroidRuntime(417): at android.widget.TabHost.addTab(TabHost.java:212)
04-09 11:11:30.316: E/AndroidRuntime(417): at com.devrepublic.kommaaropmetjeaanbieding.ui.HomeScreenTabActivity.onCreate(HomeScreenTabActivity.java:44)
04-09 11:11:30.316: E/AndroidRuntime(417): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-09 11:11:30.316: E/AndroidRuntime(417): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-09 11:11:30.316: E/AndroidRuntime(417): ... 11 more
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Which specific line is line 44 in your code?

I think this is the wrong way to approach your problem, though. If I read your problem correctly, you have 3 tabs in a TabHost, each tab is its own Activity. When you load them, all three get loaded. When the activity at position 0 gets loaded it makes a network connection, which takes time. You also want to make the tab at position 2 the default view on startup. The problem is that the activity at position 0 slows down the loading process, so it takes too long for tab 2 to become visible.

This, to me, seems like a problem of 'Don't do long-standing actions in the UI thread' issue, not a 'TabHost doesn't behave the way I need it to' problem. You should push that long-standing action into a background thread, so the UI is responsive, and while the user is busy doing other things that long process of talking to the network is being done.

The ASyncTask seems like the perfect tool for this. See this Guide (especially the bottom half) for examples on how to use it and the requirements on what is needed to keep it safe.


Steve
Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 249

Thanks for the reply Steve. But I am already using AsyncTask in my code.
In 1st Activity until doInBackground() executes, I am showing a progress bar. Thus when doInBackground() method of 1st tab Activity executes, Activity 3 gets covered by a progress bar.
I think I can accomplish my task by using some condition on call of AsyncTask in first Activity. But isn't there any way to avoid Loading first Activity at this instance?

Steve Luke wrote:Which specific line is line 44 in your code?

this is line 44-


Steve Luke wrote:I think this is the wrong way to approach your problem, though. If I read your problem correctly, you have 3 tabs in a TabHost, each tab is its own Activity. When you load them, all three get loaded.

My 2nd activity also interacts with network, but it doesn't get loaded at this point, only 1st Activity gets loaded along with 3rd.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Astha Sharma wrote:Thanks for the reply Steve. But I am already using AsyncTask in my code.
In 1st Activity until doInBackground() executes, I am showing a progress bar. Thus when doInBackground() method of 1st tab Activity executes, Activity 3 gets covered by a progress bar.

Why is your progress bar covering tab 3? It should be a component in tab 1's GUI that is visible when the network work is being done, then gets hidden (and the rest of the GUI shown) when the background task gets finished. Do any of the other components of tab 1's GUI block tab 3's view?

I think I can accomplish my task by using some condition on call of AsyncTask in first Activity. But isn't there any way to avoid Loading first Activity at this instance?

Honestly I don't use TabHost, I use a single activity and Fragments using either the ActionBar navitgation or ViewPagers for this sort of navigation. But below, don't you say that you don't load tab 2? Doesn't that mean the answer to that is 'yes' and that you already know how?

Still, I think the more responsive thing to do is load the activity, do the network connection in the background, and make it so the progress bar doesn't block your entire TabHost, rather it only displays on tab 1.

Steve Luke wrote:Which specific line is line 44 in your code?

this is line 44-


Steve Luke wrote:I think this is the wrong way to approach your problem, though. If I read your problem correctly, you have 3 tabs in a TabHost, each tab is its own Activity. When you load them, all three get loaded.

My 2nd activity also interacts with network, but it doesn't get loaded at this point, only 1st Activity gets loaded along with 3rd.
Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 249

ProgressBar is the only ui component of Activity 1 visible on Activity 3.
This is my source code of AsyncTask-

Activity 3 gets covered by the ProgressDialog created on line 1.
Please tell me if anything is wrong in my code.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Oh, you are using a ProgressDialog. A dialog is designed to pop up in front of things - by using it you are saying your intent is to overlay everything else. If you don't want to overlay everything else, don't use a ProgressDialog. Instead, use a ProgressBar in Activity 1's layout and use the AsyncTask to update that bar.
Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 249

Thanks Steve, ProgressBar works fine. But my problem is that Activity 3 gets displayed very frequently in my application. From various different activities control jumps to Activity 3 and every time when Activity 3 gets displayed, Activity 1 gets loaded and web service gets called. These irrelevant web service calls degrade performance.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

That sounds like a bigger design issue.

You need to control when the Activity makes those connections. Without seeing your code I can't be sure exactly how or what you are doing wrong. But at the very least it seems you want your network connection to outlive the Activity (when an Activity is recreated, the connection should not be disconnected and reconnected). To me, it sounds like you should create a Service to host the network connection. Make the Service a 'started' service, which lasts until stopped, rather than a 'bound' service (which lasts until the last Activity unbinds from it).

So the first time the Activity is loaded it starts the Service which (in an ASyncTask) connects to the network and provides an API through which Activities can request or send information. You can do this either through Intents or through an IBinder implementation (you will need to bind to the Service. It is okay to both Bind and Start a Service, just start before you bind). The tricky part is determining when the Service should be stopped, to preserve phone resources.

The way I have done this sort of thing (using a Service to maintain a long standing network connection), was to use the Application class to create the Service using a Start intent. Then, in the Activity's onCreate() method I bind to the same service. When the IBinder comes back I register a listener via the IBinder to keep me updated on the status of the connection. The Activity displays this status until it is complete. I then had methods on the IBinder that encapsulate the web service calls so I could do things like getBlahBlahData() instead of having to worry about the connection in the Activity code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to avoid starting default Activity in Tab