Manu13k
Manu13k

Reputation: 366

Call mainActivity's method in LifecycleObserver

I'm in trouble.

I would like to call my MainActivity's method from my LifeCycleManager ( which is a LifecycleObserver ) and perform simple XML modifications. But that crash because I'm trying to access to an XML value and main_activity.xml isn't fully created yet

First this is my LifeCycleManager :

class LifeCycleManager(context: Context) : LifecycleObserver {

companion object {
    var notif = NotificationManager()
    var onForeground = false
    var main = MainActivity()

    // If we are on MainActivity return true , otherwise return false
    fun isOnHomeController(context: Context?): Boolean {

        if (context == null || context !is MainActivity)
            return false

        return true
    }


var mContext = context

// WHEN THE APP BECOME ACTIVE
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun appComeBackFromBackground() {
    onForeground = true
    notif.cancelAllLocalNotifications(mContext)

    if (isOnHomeController(mContext)) {
        // Call the refresh dialog and print it
        main.appComeBackFromBackground("refresh", null)
    }
}
}

Here is my MainActivity method :

fun appComeBackFromBackground(status: String?, elementsadded: Int?)
{
Log.e("enterForeground", "App come back in Foreground")
if (status == null) return

when (status)
{
    "refresh" ->{
        val text = findViewById<TextView>(R.id.refreshtext)
        text.setText("test")
    }
    else -> {
        return
    }

}
}

As you can see when my MainActivity Start , my Lifecycle get the event and go directly in my appComeBackFromBackground() method. In it I'm calling my MainActivity method where I want to modify an element from activity_main.xml

That crash with theses :

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mControllers.MainActivity}: java.lang.RuntimeException: Failed to call observer method

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference

What can I do please? I really need to do it like this ( LifecycleObserver to MainActivity ). Thank's

EDIT :

I tried to call my MainActivity's method with delay like this :

Handler().postDelayed({
        main.appComeBackFromBackground("refresh", null)
    }, 5000)

And it crash too , even if MainActivity layout is loaded

Upvotes: 2

Views: 1113

Answers (1)

MRah
MRah

Reputation: 950

I am writing this in Java, (This is the method if you don't want to save the data in preferences or storage)

Application Class:

public class TestApplication extends Application implements ActivityLifecycleCallbacks {
    private static String mStringToSet;
    
    @Override
    public void onCreate() {
        super.onCreate();
        //App is not from background, a new instance of your app           is created 
        mStringToSet="new_instance";
        registerActivityLifecycleCallbacks(this);
    }

    public static String getStringToSet() {
        return mStringToSet;
    }

  
    @Override
    public void onActivityDestroyed(Activity activity) {
        if(activity instanceof MainActivity){
        //This is only called when the activity is destroyed not the application, so if the context is your desired activity then set the string here
            mStringToSet="refresh";
        }
    }

   //Implement other life cycle methods** 

  

In your activity:

public class MainActivity extends AppCompatActivity {
    private final String TAG="ACTIVITY";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        if("refresh".equals(TestApplication.getStringToSet())){
            Toast.makeText(this,"From Background",Toast.LENGTH_SHORT).show();
            (TextView)findViewById(R.id.YOUR_TEXT_VIEW).setText("refresh")
        }else{
            Log.d(TAG,TestApplication.getStringToSet());
            Toast.makeText(this,"Not from background",Toast.LENGTH_SHORT).show();
           (TextView)findViewById(R.id.YOUR_TEXT_VIEW).setText("new_instance")
        }

    }
}
     //Implement other life cycle methods
}
      

Upvotes: 2

Related Questions