Android - Navigation tab <when ​​creating a new action>

I defined HomeActivity with three tabs, and each tab is a separate activity. I used this example on the Android developer site. http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

Tab B (second tab) The user interface encodes text and a button (named Search). When the user clicks the search button, he should contact the REST web service and show the results in a list.

To do this, inside the TAB B action, by clicking the button, I call the method that creates the intent and call the new SearchResultsActivity function (hereinafter it means SRA). Inside SRA (extends the activity of the list) I have the logic to connect to the web service and parse the resulting JSON result, which displays the results as a list. I can achieve this functionality. But I see some flaws here, and my questions are:

  • Is it possible to define a new activity (SRA) for processing search results? or it would be better if it was handled in the activity of TAB B. The main reason I went to a separate activity is the SRA, which extends the ListActivity, which is needed if I want to display it as List and TabB, just expanding Activity and would not allow to display the results. So, is there a better way to do this?

  • Given the above implementation, when I navigate from TabB (search button) to SRA, the tabs are no longer visible. Since TabB triggers a new action (Intent srchIntent = new Intent (TabB.this, SearchResultActivity.class); TabB.this.startActivity (srchIntent);), the tab leaves. What would be the best solution in this case to display tabs / results in Tab B?

  • During the transition from TabB to SRA, I try to show the progress dialog / loading defined in TabB before calling StartActivity and canceling it later. But the download icon is not displayed. I tried to show the dialog and cancel in SRA as well. But the download does not appear.

+4
source share
2 answers

Hey Bala, I have to say:

1) It would be better to extend TAB B as a ListActivity and a search performed by a helper class. This way you make your code more independent.

2) Deploy 1) and you will be fine.

3) When starting a query, you should show a progress dialog and stop it when receiving results. I would use a broadcast receiver to achieve this (I can help you if you decide to do this).

0
source

There are two approaches to achieve this.

Whenever you start your other activity (for example, a search operation), before you set the activity of the search to the desired tab. You can achieve this by taking an instance of your TabActivity (i.e., Your activity that extends this class) and calling new Intent().setClass(TABACTIVITY_INSTANCE,ACTIVITY YOU_WANT_TO_SET_TO_THIS_TAB) . But make the various objects of intention be like a class of members. Do not do something like this (new Intent().setClass()) .

Declare objects without intent as there are no tabs that you hold, and then use the setClass method. (This will solve your problem with the tab disappearing)

Now, to take data from the server, I suggest you implement AsyncTask (a wonderful api is 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 ie 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 */ } } } 

Do this from the user interface thread only by calling new DownloadImageTask().execute() .

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

Hope this solves your problem.

0
source

All Articles