So much Activitys?

  • Replies:1
Seek Seek
  • Forum posts: 1

Nov 21, 2014, 10:57:25 AM via Website

Hello,

im new with in this Forum and with android coding and want first to say hello to everyone here :)
(sry for my poor english)

I use eclipse with the sdk, because android studio haves now to many bugs and hangover to do something. To start android i am trying to build an app with can control a admin gui (Website) from my hoster. I build this using get and post to the httpserver and an http Parser, Login and some parts are already done.

Now im seeing that my acitivitys are growing for each Change or Infosite of the Website. Is it normal that some applications haves 15+ Activitys or maybe is there a better solution (show/hide Containers of gui elements?), generate it by the programm code?

The Secound Question is i use asynctask and i try to build a new class for asynctask for the get and post methodes i use in nearly every activity. So ican save sourcecode in each Activity. I havent find a good solution how i can update the ui when asynctasc is complete and i want to start a function from the ui thread.

I have trial to use globalvariable and a broadcast reciver but this just work in non-threated classes. I am searching now over 2 days goolge to find a good solution, without to intigread asynctask in every activity and use onPostExecute Methode.

hope my english is understandable :)

thanks

best regards,
Seek

Reply
Kevin Berendsen
  • Forum posts: 118

Nov 21, 2014, 12:05:20 PM via Website

Hey Seek :)

Welcome to this forum and especially this section of the forum.

15+ activities is a bit too much indeed. I recommend you to use Fragments. You could see Fragments as embedded content which you can include into your Activity dynamically. This way, you save time to finish and start a new activity which could be confusing some end users and I believe this is what you mean by containers and such.

Take a look here at the training and documentation of fragments: http://developer.android.com/guide/components/fragments.html

For Global variables, you could use SharedPreferences. It saves a variable even when you close your app and could be used instantly in every activity/fragment that you have.

It's great to hear that you already know how to use AsyncTasks. There are several ways to get variables into an AsyncTask extended class. The way I do it is to use create a constructor of the new AsyncTask class and store the variable locally in the class which can be used in onPostExecute. By the way, onPostExecute is working on the main(UI) thread so you can access UI elements in your new AsyncTask class if it's an inner class of a fragment or activity.

Let me show you an example of the constructor.

class NewTask extends AsyncTask<Type, Type, Type> {
     TextView textView;

     public NewTask(TextView tV) {
          this.textView = tV;
     }

     protected type doInBackground(Type... name) {
          // do some stuff here like fetching data.
     }

     protected void onPostExecute() {
          textView.setText("It worked!");
     }
}

The way to use it:

TextView textViewOutOfAsyncTask = (TextView) findViewById(R.id.textViewId);
new NewTask(textViewOutOfAsyncTask).execute();

Only onPostExecute works on the main/UI thread, therefore changing the UI in the doInBackground method will simply cause an exception or something else, I honestly never tried it.

For SharedPreferences, take a look at this: http://developer.android.com/training/basics/data-storage/shared-preferences.html

I hope this helps you out to continue your journey of developing your app :)

Seek Seek

Reply