light weight alternative to Activity - android

I have a situation for each i am trying to find a better (read optimized) pattern to employ.
Essentially, i have three activities - LaunchActivity, WelcomeActivity and MainActivity.
The LaunchActivity is the DEFAULT LAUNCHER activity and in my case, LaunchActivity does not show any UI i.e i don't call setContentView() at all in onCreate(). All that i am doing is, essentially, in onStart(), i check certain conditions and based on the result, either launch WelcomeActivity or MainActivity.
Now, i am wondering, should i really use an Activity [LaunchActivity's superclass] do some checks? Is there a light weight option that i could use to quicken the launch process since instantiating an Activity could be fairly time consuming and expensive?
Thanks all.

I use activity acting as a splash screen in my applications while dealing with such kind of scenarios.The benefit of doing this is that it is giving my application a nice interface, and a graphically rich promotion, and also in that splash Activity,I am taking decisions that what activity should be started next on the basis of last saved state of my Android Application.
Another alternative(which i will not implement in any application developed by me) is to start the Welcome Activity everytime,and in its onCreate() Check some condition.If that condition is met then, open the MainActivity by using intents...else carry on with the flow of the welcome Activity
public class WelcomeActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(your condition)//check some condtion here
{
// if met,go to MainActivity
}
else
{
//carry on with the flow of WelcomeActivity
}
}
}
Overall, I believe using the first approach is better way , because it is just adding an overhead of only one activity,but making the flow of the application cleaner

Related

how to know our app has gone to background in android

When user goes to other apps from my app or presses device home button from my app or etc , then my app will be sent to background. And in the background am doing some stuff . So how to know is my app has gone to background . One solution will be to check if any of app activities are in onstart,onrestart,onresume,onpause,onstop if not then we can consider it in background. But this solution is tedious please help me if you have a easy solution
Using onPause isn't tedious, and may be your only solution anyway. Create an abstract Activity class that all your Activity classes extend and do whatever you need to do in onPause there:
public abstract class BaseActivity extends Activity {
#Override
public void onPause() {
super.onPause();
// Do whatever you need to do in onPause here
}
}
Using this example, all your Activity classes should now extend BaseActivity.

Android: How to call a a Class within the Current Class

I'm working on an application that has multiple Activities, and I'm tired of running back and forth between the Manifest and XML layout and stuff. Is there a way to have
Intent intent = new Intent(MainActivity.this, MainActivity.Settings.class);
Or something? Because I've tried it, it doesn't throw me an error, but it just force closes the application. I'm able to bundle all my classes from different .java into one, for ex.
public class MainActivity extends Activity
{
...
#Override
protected void onCreate(Bundle MainActivityState)
{
...
}
public class Settings extends ...
{
...
}
public class Register extends ...
{
...
}
public class Login extends ...
{
...
}
public class BeautifulLady extends personality ...
}
Simple. Just don't even try.
An activity loosely represents a single screen - something the user interacts with. Android is built around this concept and trying to circumvent it will lead to tears.
Stick with it. Having your classes in separate files, and having layout XML separate for each activity, will become your friend and will actually speed things up once you are familiar.
Start with the Activity life cycle document and read it several times until the penny drops. Then expand out from there.
http://developer.android.com/reference/android/app/Activity.html
Object oriented programming, with classes that take care of themselves, is a joy and regardless of which platform you choose to develop on is the way to go for the foreseeable future (old hands, no debates on OOP vs functional please ;)).
If you are going to do mobile development, then the separation of activities, classes and UI is the same concept, just done differently.
See also MVC programming and its' cousins.
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
Good luck.
Perhaps you can define your Activity as 'Single Top', then launch your activity from herself like MainActivity.this.startActivity(new Intent(MainActivity.this, MainActivity.class). It'll then go into onNewIntent() and you will redisplay what you want to redisplay. This way you will have only one screen.

Need Suggestion to do a task when Android Boots-Up

I have written an activity A, when users press a button, it will do MyConfig.doSomething() where MyConfig is simple class with activity A passed to it.
public class A extends PreferenceActivity {
private MyConfig mMyConfig;
/* pseudo code, when button clicked, call */
mMyConfig.doSomething();
}
In mMyConfig, it accesses SharedPreferences for some configuration. Thus, I can do this to pass the activity to mMyConfig for calling getSharedPreferences().
mMyConfig = new MyConfig ( this );
Here comes my request:
I want to do something that MyConfig.doSomething() already does, but except when users click some button to invoke it, I want to invoke it when Android Boots-Up.
I can write another class to extend BroadcastReceiver and then starts activity A by calling startActivity(A.class), and then in A, do some tricks to make mMyConfig.doSomething() happen. It works but the Application will be shown on screen when Android Boots-Up.
I want to make mMyConfig.doSomething() happen implicitly without letting users be aware of it. I suppose two possible solutions but I don't know how to do it.
Solution A:
Write a class that extends BroadcastReceiver, start a service (instead of activity A) that reads the SharedPreferences of A and create MyConfig object to do doSomething(). However, I don't if this can work if activity itself is never launched and how could I do this (read SharedPreferences from a service)?
Solution B:
Write a class that extends BroadcastReceiver, start activity A without showing it, put it to activity stack by calling startActivity(A.class) in onReceive. Is this possible?
Instead of Activity, which are meant to be visible to the user, you can make your BoardcastReceiver to start a Service instead. It is meant to perform tasks in the background without disturbing the user. The official guide is a nice place to start with.
Edited:
To access the SharedPreference of your application, simply call this line inside your service:
SharedPreferences pref = PreferenceManager.getSharedPreferences();

Android equivalent of: void main() / Sub Main?

I am trying to make my Android application run some code when run from the launcher, BEFORE launching into an activity. That is to say I want my app to start with a Sub Main as opposed to going into an Activity first.
Essentially, in pseudo, I want to do something like this:
void main() {
doSomeInitializationStuff();
startActivity(myFirstActivity);
}
According to this question, it looks like Android does not have this concept literally. So I was looking at creating an invisible Activity as my entry point, but cannot figure out how to make an activity invisible. I've tried these two methods, which seem to be the only ones coming up in my searches, but they don't seem to actually do anything...
this.setVisible(false);
this.setTheme(android.R.style.Theme_Translucent_NoTitleBar);
Instead of creating an invisible activity you can create a splash activity and start all your initializations there .
I have not tried this but you can extend the application class and use onCreate in your application class to initialize what you need.
Here is the JavaDoc for onCreate of the application class
/**
* Called when the application is starting, before any other application
* objects have been created. Implementations should be as quick as
* possible (for example using lazy initialization of state) since the time
* spent in this function directly impacts the performance of starting the
* first activity, service, or receiver in a process.
* If you override this method, be sure to call super.onCreate().
*/
public void onCreate() {
}
You will need to let the app know that you are using a custom application class by using the the android:name parameter in the tag of the Android manifest file.
In most of the Android application there is concept of SplashScreen one can use that screen to accomplish such behaviour and the real motif is of this SplashScreen is to proccess such tasks in background while advertising for the app itself and various things related to that
One option would be to not have an invisible Activity, but a SplashScreen. This has the advantage that the user already sees something happening when the app starts up, so that he does not get the impression it is no working. For an example see e.g. this class; you would put the doSomeInitStuff() at around line 54
Otherwise I think, you can just not load a layout in onCreate() of the first activity and then forward from there.

Is there a way to make a function that's available to all Activities?

I have a set of commands that I want my app to run when it's restarted, regardless of what activity it's on. I know I need to put an onRestart() into every one.
Since it's the same set of commands regardless of what activity it's on, is there a way I could have them all refer to a single function for that? It seems like that would be better then having to copy paste the commands into each onRestart() handler. It will be a lot less work if I need to change the set of commands too.
You have a couple of options, depending on the code.
You can put it in a helper class as a static function: public static void doWork() { .. } This should work, unless whatever you are doing depends on being in the activity. You can generally just pass it what it needs though, like the Context.
Or, you could extend Activity with your own class, MyActivity and place the work in that onResume. Then extend MyActivity for each of your real activities. They will now automatically do that work when you call super.onResume(). This works well as long as you really want to do the same thing in every activity, and don't use a lot of specialized activities like ListActivity.
Edit:
public class MyHelper {
public static void doWork() {
// do your work here
}
}
public class MyActivity extends Activity {
public void onResume() {
super.onResume();
MyHelper.doWork();
}
}
A search for "static method" will provide more details.
Derive all your activities from a single class (something like ActivityBase) that, in turn derives from system-provided Activity. Then implement onRestart() in ActivityBase.
Is it your application that is restarting from scratch? Or just your activities that are restarting/resuming?
The Application class has an onCreate() method, and you can extend Application in your app to override its behavior. Just remember to change the name of the application in AndroidManifest.xml so it picks up your custom Application class when starting. This code would run before any activities start up. But it won't run every time an activity is stopped and restarted. If that's what you need, this won't do it.
You could also implement a singleton class that contains an initialize() method, or restart() method. You simply call it from onRestart() in each activity you want it in. It sounds like this special code ought to be localized away from your activities so I don't think I'd recommend extending Activity to put the code there.

Categories

Resources