1

enter image description here

As in my algorithm, how to make Application.java only run in Main menu ONLY and as for now, MyApplication.java run on every activities. i already try this on my main menu, but did not work.

@Override
    protected void onResume() {
        super.onResume();
        SystemRequirementsChecker.checkWithDefaultDialogs(this);
        beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
            @Override
            public void onServiceReady() {
                beaconManager.startRanging(region);
            }
        });

    }
    @Override
    protected void onPause() {
        super.onPause();
        beaconManager.stopRanging(region);
        finish();
    }

    @Override
    protected void onStart() {
        super.onStart();
        beaconManager.stopRanging(region);
    }

    @Override
    public void onStop(){
        super.onStop();
        beaconManager.stopMonitoring(region);
        beaconManager.stopRanging(region);
        finish();
        System.runFinalizersOnExit(true);
        System.exit(0);
        android.os.Process.killProcess(android.os.Process.myPid());
    }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
borneo
  • 97
  • 9

2 Answers2

2

I’d strongly suggest you revisit Android app fundamentals and The Lifecycle Activity, as it looks like you could brush up on some core Android concepts. You also want to check why exiting apps on Android is frowned upon.

As the docs say, the Application is a base class for maintaining global application state and is instantiated before any other class when the process for your application/package is created. Therefore, you can’t just disable it.

If my understanding is correct, you want to represent a state of sorts, which denotes the user has passed all the initial requirements (login and IMEI check) and now has full access to the app functionality. Ignoring the contemporary tools we have at our disposal for a second, a very basic approach could be a simple class offering main menu functionality, e.g.:

class MainMenu {
  private final Context appContext;
  private final BeaconManager manager;

  public MainMenu(@NotNull Context context) {
    appContext = context.getApplicationContext();
    manager = BeaconManager.getBeaconManager(context);
  }

  public void connectBeacon(@NotNull Callback callback) {
    beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
      @Override
      public void onServiceReady() {
        beaconManager.startRanging(region);
        callback.onSuccess(region);
      }
    });
  }

  public void disconnectBeacon(@NotNull Region region) {
    if (manager.isRanging(region)) {
      beaconManager.stopRanging(region);
    }
  }
}

which you create in MainMenuActivity or a base class for each Activity instance after a successful login.

A better alternative, especially if your app is larger than a few classes, is to follow an architectural pattern. If you can’t decide which, you can check Android Architecture Components. Another useful pattern/technique to consider is Dependency Injection - with a library such as Dagger2, you could utilise subcomponents or scopes to achieve exactly the separation you’re after.

Tadej
  • 2,901
  • 1
  • 17
  • 23
  • Yes. i want beacon ranging detect only in Main Menu and others, except Login and Imei check. so, i'm just declare ConnectBeacon and disconnectBeacon at every activities ? except login and imei ? , for what happen in my Application class is i am following a tutorial and ended in my algorithm, and i am confused with my own. now i have bit bright clearer view after your explanation. thank you – borneo Feb 26 '18 at 01:27
1

Basically you can not, Application Class created as you run the app (even in backgrand) .

this is an overview from github to introduce ApplicationClass

The Application class in Android is the base class within an Android app that contains all other components such as activities and services. The Application class, or any subclass of the Application class, is instantiated before any other class when the process for your application/package is created.

full description

github

Radesh
  • 13,084
  • 4
  • 51
  • 64