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.
Related
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
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();
I have a one notification service which fires at every 5 seconds and get the data and parse it and then display it..
But this mechanism works only for the home screen of the app.
I want this to be done in every screen of the app.For that should i write the same code in every activity of the project ? It is very tedious job . I want to make it generalize ..
How to do that ?? any ideas??
you can make BasicActivity and have method in that Activity and call that method inside onCreate();
now extend every Activity in your project from your BasicActivity
If you are talking push notification service, this idea seems to problematic. tell us more what are you exactly trying to do.
you do something like this in some utility class
public static void showNotifction(Activity a)
{
//notify
}
and over this if you have any call back methods.. Then create an interface with those methods and implement them in all those Activities and you can do this..
public static void showNotifction(CommoInterface a)
{
if(a insatncof Activity){
//notify
a.callBack(); // callBack is a method in the interface..
}
}
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.
I would like to run a piece of code every time any activity or service is started. In this piece of code, I might do things such as set the default uncaught exception handler and manage log levels.
The problem is that I have an activity which starts with the user clicking the application icon. I have another which starts if a certain intent is broadcasted, possibly from another app and possibly called before the user click the launch icon. Same goes for services.
I need to guarantee that a certain piece of code will be run while keeping the code clean; that is to say, without having to manually add that snippet of code to every activity and service class that I have.
Could you not extend the basic Activity class for Android like this:
public class MyClass extends Activity {
public void onCreate(Bundle bundle) {
//Add custom code here
}
}
Then have all of your actual "Activity"'s in your application extend the custom class?
public class MyInterfaceClass extends MyClass {
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
//Other code here
}
}
That way all your custom code will be called when the Activity starts up.
For an application called Wibble...
public class Wibble extends Application {
protected static void DoSomething()
{
// Do your common code here
}
}
Then extend Activity...
public class WibbleActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Wibble.DoSomething();
}
}
Then derive all activity classes from WibbleActivity...
public class Whatever extends WibbleActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// WibbleActivity calls Wibble.DoSomething()
// so the 'Whatever' class doesn't have to.
}
}
Any Activity derived from WibbleActivity will run Wibble.DoSomething() implicitly.
If you want Services in the mix I can't help - I'm an Android 'rookie' and haven't got on to Services yet but I suspect extending your own app-level Service class might work in the same way.
You could extend Application and do it in its onCreate() method.
You have two choices
A) You can manually add the code - it might be only two lines importing and instantiating something from a source file you copy in unmodified - to every separate component that you write. It will only be in your projects, not in other people's unless they do it too.
B) You can, after no small difficulty, learn to make your own custom version of android that automatically does this each time it starts up a suitable component, and install this customized version on developer phones or hacked consumer phones.
"started" is ambiguous - what are you referring to? onCreate? onResume?
In any case, your best bet is to have a separate class with a static method that does the code you are talking about, and you call that in every single onCreate (or onResume, whichever you need) of each one of your activities.
That, or you create your own Activity subclass and derive all your activites from it, and override onCreate (or onResume). All your onCreate/onResume implementations are required to call the superclass' implementation, so you're guaranteed to have your code caled.