I am new to Android development and fooling around with some sample programs to get better acquainted with it.

One of the tutorial books I read recommended writing a class to extend the Application class for easy access to shared preferences and things.

My application includes a login screen and I have my application class handling all the login/logout methods (don't know if this is the best way to do it, but that’s a different issue I guess). In my logout method I created an intent to open the Login screen activity after the user logs out and got the error: "android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?"

I know how set the flags to fix this, but I was wondering if this is bad practice. I could guess at why it might be bad to do but I don’t really have a deep enough understanding to know for sure.
Should I only start activities from inside other activities or is it ok to do something like this? If not, why exactly?


use intents to start new activity,
Intent intent = new Intent(yourcurrentclassname.this, secondActivityClassname.class);

Intent are like triggers to activate the requred activity, and they will work as mail mans to send and retrive the information or avtivities or views.

Yes, I realize that. I know how to start an activity. I was asking if it is bad programming practice to send an intent to start an activity from outside an activity context (another class). An error pops up when you try, but there is a way to override it. My question was not how to start an activity, but if this is a bad way to do it. Sorry If my question wasn't clear.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.