-1

How do I call recreate() from another class in Kotlin? I am used to Java and did it like in the code shown below for some UI changes.

public class MainActivity extends AppCompatActivity {

    private static MainActivity instance;

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

        instance = this;
    }

    public static MainActivity getInstance() {
        return instance;
    }
}

public class SecondActivity extends AppCompatActivity {

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

    private void onClick(View view) {
        MainActivity.getInstance().recreate();
    }
}

Would be great if somebody knows how to achieve that in Kotlin, I am pretty much stuck with this. Beste regards, Markus

Markus
  • 129
  • 1
  • 8

2 Answers2

0

What @broot answered above is right for a 1:1 port but the practice is entirely wrong. What you are doing here is essentially creating a strong reference to an activity that isn't even shown to a user anymore.

What you should do however is something like the following:-

Inside Second Activity

 val intent = Intent(BROADCAST_RECREATE_MAIN)
 LocalBroadcastManager.getInstance(this@ SecondActivity).sendBroadcast(intent)

Inside Main Activity

private val localBroadcastReceiver by lazy {
  object : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
      \\Recreate main here
    }
  }
}

Inside MainActivity's onCreate

val intentFilter = IntentFilter()
intentFilter.addAction(BROADCAST_RECREATE_MAIN)
LocalBroadcastManager.getInstance(context)
        .registerReceiver(localBroadcastReceiver, localBroadcastIntentFilter)

And inside onDestroy

LocalBroadcastManager.getInstance(requireContext())
        .unregisterReceiver(localBroadcastReceiver)

   
Rahul Rawat
  • 360
  • 2
  • 9
-1

A 1:1 port to Kotlin would be something like:

class MainActivity {
    protected fun onCreate() {
        instance = this
    }

    companion object {
        lateinit var instance : MainActivity
    }
}

class SecondActivity {
    private fun onClick() {
        MainActivity.instance.recreate()
    }
}

But it really looks like a bad idea. You should use one of available ways to communicate between activities, not recreate it like this. I guess this is not what you asked though :-)

broot
  • 21,588
  • 3
  • 30
  • 35
  • Thanks for your answer! What would you suggest as a proper way to achieve that? I need this activity active while all other allready created activities get recreated (for UI changes). What might be the major downside of using it this way? – Markus Jun 25 '21 at 21:49
  • I won't suggest a specific solution as I'm not familiar enough to Android. It just doesn't feel right to store an activity anywhere, because activities are managed by the framework. Also, components of Android don't usually communicate directly - you don't instantiate an activity and display it, but you send an intent. I guess it would be cleaner to open some kind of communication channel or event bus between activities and send a command to recreate itself. But ss I said, I'm not sure what would be the best here. – broot Jun 25 '21 at 22:09