views:

59

answers:

1

I am creating a Splash screen(Activity) in my Android app. I have an inner class which extends AsyncTask. The constructor for this inner class accepts a Context object, which is being sent inside the Splash activity's onCreate method. There is also reference to a constant String field.

public class Splash extends Activity {
private static final String TAG = "Splash";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    new SplashAsyncTask(this).execute(new Void[] {});
}
private class SplashAsyncTask extends AsyncTask<Void, Void, Void> {
    private Context context;
    public SplashAsyncTask(Context context) {
        this.context = context;
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            Thread.sleep(1500);
        }
        catch(InterruptedException e) {
            if(Constants.LOGGING)
                Log.e(TAG, e.toString());
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
    }
}
}

what scope should i be using for the private static final String TAG = "Splash"; variable? all scopes seem to work fine, but what is proper/more efficient? I notice that eclipse automatically creates the variable with the public scope declaration

Also, Do i really need to send the Context object to the SplashAsyncTask constructor? or can i/should i just use Splash.this in all cases where i need a Context reference?

+1  A: 

Hi, private and public only indicate how your can access your variable, it is more a security thing, ideally all variable of a class should be private and you should access them by a getter and setter method.

I think the word static is more importante here because making a variable is a big thing, it means that the variable can be accessed without making an instance of the and the all share this variable what i mean if you would have made the variable:

public static final String TAG = "Splash";

then you could access it by writing:

Splash.TAG

without making an instance of it, but since its private you can´t but it is still a shared variable with all the instances you make of your class Splash

hope this helps

mklfarha