0

I have two activities, LoginActivity and MainActivity. When I press a button in my LoginActivity I want to call a function in MainActivity.How can I achieve this?

  • MainActivity function*
    fun triggerRestart(context: Activity) {
        val intent = Intent(context, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        context.startActivity(intent)
        if (context is Activity) {
            (context as Activity).finish()
        }
        Runtime.getRuntime().exit(0)
    }

Please give me a proper solution. Thanks

3
  • Activities shouldn't talk to each, each Activity is consider its own separate entry point. Is there a reason you can't run the function in the login activity? Commented Mar 28, 2021 at 8:30
  • @Shawn, It's a common practice to store static functions which doesn't access anything of the particular activity it is declared in but are only related to it in its companion object, so it can be re-used through other Activities instead of re-writing it in Activities it is used. Although, the OP can also use extension function on Activity but there's nothing wrong in the way it is currently used. Because, in this particular case, MainActivity is running or not doesn't matter and it cannot cause a runtime error. This is done to simplify the code and make it easier to understand the code. Commented Mar 28, 2021 at 9:50
  • @Shawn I want to run a function to clear the app data and the function I am running is causing to close my app after clearing app data. that's why I want to restart it back. So I think it is not possible to restart the app with a single activity. Commented Mar 28, 2021 at 10:13

2 Answers 2

1

You can pass data from one activity to another using extras. For example you can set some flag in first activity by doing this:

val intent = Intent(this, MainActivity::class.java)
intent.putExtra("SOME_KEY", true)
startActivity(intent)

Then receive the flag in onCreate of second Activity and call method if true flag is passed:

val flag = intent.getBooleanExtra("SOME_KEY", false)
if (flag) {
  //call method in second Activity
}

However you shouldn't be calling Runtime.getRuntime().exit(0) in first place. You can clear app state by just recreating Activity. Also casting to activity class is unnecessary, because you already pass activity instance to the method:

fun triggerRestart(context: Activity)

so context object is an instance of Activity and there is no reason to check and cast it. Instead of this:

if (context is Activity) {
  (context as Activity).finish()
}

just call

activity.finish()
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer is great and easy to understand thanks for your contribution.
0

There are 3 ways to do it in Kotlin:

  1. Using Object - As it is a static function which doesn't access views or data of MainActivity, you can call it through an object of MainActivity as it need not be the running instance.

    So, you can call it as MainActivity().triggerRestart().

  2. Using Companion Object - You can also declare the function in Comapnion Object of MainActivity as

    Companion object {
        fun triggerRestart(context: Activity) {
            val intent = Intent(context, MainActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(intent)
            if (context is Activity) {
                (context as Activity).finish()
            }
            Runtime.getRuntime().exit(0)
        }
    }
    

    And then can access it as MainActivity.triggerRestart().

  3. Getting instance from Companion Object - You can get an instance of the MainActivity and then, can access the function through it as:

    Companion object {
        val instance = MainActivity()
    
    }
    
    fun triggerRestart(context: Activity) {
        val intent = Intent(context, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        context.startActivity(intent)
        if (context is Activity) {
            (context as Activity).finish()
        }
        Runtime.getRuntime().exit(0)
    }
    

    and then can access it as MainActivity.instance.triggerRestart().

Difference between first and second is that you don't have to unncessarily create an object of MainActivity in second way to access a static function.

Difference between second and third is in third, you're accessing the function through an instance of activity passed by the activity itself, unnecessary for static values but important in case you want to access the views/values of running instance of the MainActivity.

You can further improve the third way to ensure that it passes only the running instance and not new instance. For that, create a private temp var and intialize it as this(which means Activity) in init() of the class and pass it as the instance. This way, third will pass the running instance of the activity.

1 Comment

Thanks I managed to apply your solution and it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.