开发者

Android - Tab Navigation <while Spanning a new activity>

I have defined a HomeActivity with three tabs and each tab is a seperate activity.I used the example in the android developer site. http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

Tab B's (second tab) UI conatins a text and button (named Search).When the user clicks the search button,it should contact a REST webservice and show the results as a list.

In Order to do this,inside TAB B activity,on the click of the button,i call a method which creates an intent and calls a new SearchResultsActivity(referring as SRA henceforth).Inside the SRA(extends List Activity),i have the logic for connecting to the webservice and parsing the JSON result returned which displays the results as a list.I am able to achieve this functionality.But i see some drawbacks here and my questions are:

  • Is it fine to define a new activty (SRA) to handle the search results? or would it be better to have it handled in TAB B activity itself?The main reason why i went for a seperate activity is,the SRA extends ListActivity which would be needed if i want to display it as List and TabB is just extending Activity and wouldnt allowme to display teh results.So,is there a better way to do this?

  • Given the above implementation,when i navigate from TabB (click search button) to SRA,the tabs are not seen anymore.As TabB is calling a new activity( Intent srchIntent = new Intent(TabB.this, SearchResultActivity.class); TabB.this.startActivity(srchIntent);),the tab goes away.What could be the best solution in this case so that the tabs appear/results shown within Tab B ?

  • While navigating from TabB to SRA,i am trying to show a Progress dialog/Loading defined in TabB before calling StartActivity and cancel it afterwards.But the loading icon does not appear.I have tried showing the dialog and canceling in SRA as well.But the loading does not appear.

  • 开发者_运维知识库


Hey Bala, what i have to say is:

1) It would be best to extend the TAB B as a ListActivity and the search done by a helper class. This way you are making your code more independent.

2) Implement 1) and you will be ok.

3) You should show a progress dialog when you start the request, and stop it when you got results. I would implement a broadcast receiver to achieve that (I can help you out, if you choose to do that).


There are two approaches to achieve this goal..

Whenever you make start your another activity (i.e. search activity), just before that set your search activity to your desired tab. You can achieve this by taking a instance of your TabActivity (i.e. your activity which is extending this class) and calling new Intent().setClass(TABACTIVITY_INSTANCE,ACTIVITY YOU_WANT_TO_SET_TO_THIS_TAB). But make to make the different objects of intent as member class .. Don't do something like this (new Intent().setClass()).

Declare the no of intent objects as no of Tabs you are holding and then use setClass method. (This will solve your problem of the tab disappearing)

Now for taking data from server, I suggest you to implement AsyncTask (wonderful api available on Android):

private class DownloadImageTask extends AsyncTask<String, Void, String> 
{
    AbousUsHandler aboutHandeler;

    @Override
    protected void onPreExecute()
    {
        mProgress.setMessage("Please wait");
        mProgress.show();
        super.onPreExecute();
    }

    protected String doInBackground(String... urls)
    {
        /* Do you your background task i.e. getting data from server
           but don't do ui related things here as this method is called in Thread
           pool not on Android Ui thread.*/

        return null;
    }

    protected void onPostExecute(String result)
    {
        try
        {
            mProgress.dismiss();
            /* set Your List View Adapter here */
        }
    }
}

Execute this from your UI thread only by calling new DownloadImageTask().execute().

First preExecute will be called and then doInBackground and when you get your data from the server, onPostExecute will be called.

I hope this solves your problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜