Search code examples
androidandroid-activitytimertasknon-static

Restart activity on command received from Firebase


I'm trying to restart my main activity on a command received from Firebase. Ive managed to make it work but in MainActivity class has a TimerTask and in order to call the Stop function from my Firebase class I have to make a new instance of the MainActivity Class. This will also create a new instance of the timer. The command will stop the timer (the new initiated one) but the old one keeps running in the background. How can I call the stop function in the MainActivity withouth creating a new instance. Its a non-static function so I cannot call it from a static context.

Here's my Code snipet if it helps.

    if (remoteMessage.getNotification() != null) {
        String msg = remoteMessage.getNotification().getBody();
        Log.v("test", "FB msg is: " + msg);
        if (msg.equals(COMMAND)) {
             Intent i = new Intent(MyFirebaseMessagingService.this, DashboardActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(i);
            dashboardActivity.stopTimer();


           // SharedPrefManager.getInstance(getApplicationContext()).storeTimer("false");


        } else {
            Log.v("test", "unknown");
        }
    }

Ultimately, I want to send a command from Firebase that will force my MainActivity to restart completely.


Solution

  • I used BroadcastReceiver and it worked. Follow this example here