2

I'm trying to call a method when a user launch my application (no matter it is a fresh launch, or a return to it after hiding by home buttton)

For iOS, we can put the method in "applicationDidBecomeActive" in AppDelegate.m, so the method will be called when app launches.

However, for Android, if we put the method in onResume() in the 1st activity, not only app launch will call the method, backing to the the 1st activity from other activities in the same app will also call the method. I don't want this happen, I just want the method to be called when app launches.

I've asked it in the past but seems no answer on Android.

Any solutions? Thanks a lot.

Community
  • 1
  • 1
Kit Ng
  • 993
  • 4
  • 12
  • 24
  • then you should not have accepted the partial answer. Or you needed to ask two different questions, each for ios and android. – Anoop Vaidya May 17 '13 at 13:18

5 Answers5

2

You can implement your own Application class. MyApplication extends Application and set it as your Application in the manifest file AndroidManifest.xml.

 <application 
  android:name="MyApplication" 
  . 
  . 
  .  >
  </application>

In MyApplication Class, implement onCreate() and onTerminate() methods.

onCreate() method called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created.

See the docs for Application.

Manish Dubey
  • 4,206
  • 8
  • 36
  • 65
  • If an app is resumed from the background(not re-created by the system) - the ``onCreate`` method of the Application will not be called. – makovkastar May 17 '13 at 13:36
  • I'm tried this method but it seems there's some restrictions. As the class extends "Application", so my method no longer works as it requires the class is extending "Activity" – Kit Ng Jun 01 '13 at 21:09
0

You can define a superclass for all your activities and track the state of the app. If all activities are in stopped state - app in the background, otherwise - in the foreground. In onStart() and onStop() methods of your super activity you can increment and decrement the number of visible activites. Then in onStart() check if there was any currently visible activites. If no - app becomes active and you can call your method:

public class SuperActivity extends Activity {
    private static int mVisibleActivitiesCount;

    @Override
    public void onStart(){
        super.onStart();
        if (mVisibleActivitiesCount == 0) {
            onAppBecomesActive();
        }
        mVisibleActivitiesCount++;
    }

    @Override
    public void onStop(){
        super.onStart();
        mVisibleActivitiesCount--;
    }

    private void onAppBecomesActive() {
        // Do some staff
    }
}
makovkastar
  • 5,000
  • 2
  • 30
  • 50
0

There is no method that is called only when the app returns from background, but you could implement something that you could implement something like this to see if the app started from background or it was first started. Create a general activity that will be extended by all the other activities and override onStart():

public abstract class CustomActivity extends FragmentActivity {

public static int ACTIVITIES_RUNNING = 0;

    @Override
protected void onStart() {
    super.onStart();
    if (ACTIVITIES_RUNNING == 0) {
        //app came from background
        //start whatever you want
    }
    Const.ACTIVITIES_RUNNING++;
}

}

bogdan
  • 782
  • 3
  • 7
0

First create singleton for counting activities in foreground

public class ActivitiesCounter {

    public interface ApplicationLaunchListener{
        public void onLaunch();
    }

    private int mCounter = 0;

    private ApplicationLaunchListener mListener;

    private static ActivitiesCounter mInstance = new ActivitiesCounter();

    public static ActivitiesCounter getInstance(){
        return mInstance;
    }

    public void increase(){
        if(mCounter == 0){
            if(mListener != null){
                mListener.onLaunch();
            }
        }
        mCounter++;
    }

    public void decrease(){
        mCounter--;
    }

    public void setApplicationLaunchListener(ApplicationLaunchListener listener){
        mListener = listener;
    }

} 

Then activity:

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ActivitiesCounter.getInstance().setApplicationLaunchListener(new ActivitiesCounter.ApplicationLaunchListener() {
            @Override
            public void onLaunch() {
                Toast.makeText(MyActivity.this, "launched", Toast.LENGTH_LONG).show();
            }
        });

        findViewById(R.id.btn_activity_b).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyActivity.this, ActivityB.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        ActivitiesCounter.getInstance().increase();
    }

    @Override
    protected void onStop() {
        ActivitiesCounter.getInstance().decrease();
        super.onStop();
    }
}

Activity B also should increase and decrease counter

public class ActivityB extends Activity {

    @Override
    protected void onStart() {
        super.onStart();
        ActivitiesCounter.getInstance().increase();
    }

    @Override
    protected void onStop() {
        ActivitiesCounter.getInstance().decrease();
        super.onStop();
    }
}

It's better create BaseActivity (with onStart, onStop) for all activities in your app (then, you don't need override onStart, onStop every time)

bendyna
  • 166
  • 1
  • 6
0

Open the androidManifest.xml from the Manifests Folder

Choose the Activity you want to open when app start

Add these codes to that Activity

 <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>

Remember to removes this code from the activity in which its already present(usually MainActivity)

Sourav
  • 139
  • 1
  • 1
  • 9