0

My app has two button, first button will show a new Activity on second screen, and second button will update that Activity with some data.

But once the second Activity started, I cannot startActivity again. Also, I cannot kill the second Activity. How can I kill the Activity and then restart it?

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.btnShowScreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSecondDisplay();
            }
        });

        binding.btnSetData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSecondDisplayWithData();
            }
        });
    }

    private void showSecondDisplay(){
        DisplayManager displayManager = (DisplayManager)getSystemService(Context.DISPLAY_SERVICE);
        Display[] displays = displayManager.getDisplays();
        if(displays.length > 1){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                ActivityOptions options = ActivityOptions.makeBasic();
                options.setLaunchDisplayId(displays[1].getDisplayId());
                startActivity(
                        new Intent(this, MainActivity2.class),
                        options.toBundle()
                );
            }
        }else{
            Toast.makeText(this,"No second display", Toast.LENGTH_SHORT).show();
        }

    }
    private void showSecondDisplayWithData(){
        DisplayManager displayManager = (DisplayManager)getSystemService(Context.DISPLAY_SERVICE);
        Display[] displays = displayManager.getDisplays();
        if(displays.length > 1){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                ActivityOptions options = ActivityOptions.makeBasic();
                options.setLaunchDisplayId(displays[1].getDisplayId());
                Intent intent = new Intent(this, MainActivity2.class);
                intent.putExtra("data", "this is data");
                startActivity(intent, options.toBundle());
            }
        }else{
            Toast.makeText(this,"No second display", Toast.LENGTH_SHORT).show();
        }

    }
}


public class MainActivity2 extends AppCompatActivity {

    private ActivityMain2Binding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMain2Binding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String data = extras.getString("data");
            binding.viewData.setText(data);
        }
    }
}
CL So
  • 3,647
  • 10
  • 51
  • 95
  • Y you want kill the second activity... What was the problem!? – Gobu CSG Jul 07 '22 at 10:37
  • Because I cannot restart the activity if I don't kill it. – CL So Jul 07 '22 at 14:55
  • Why you're checking display!? And I can't understand startActivity will launch your Activity... Y you're checking display doing something more in the methods – Gobu CSG Jul 07 '22 at 15:00
  • Because the MainActivity2 must show on second display, if user doesn’t connect the phone to second display, the app should prompts error message. – CL So Jul 07 '22 at 16:29
  • Display!? Which one... Like foldable phone or HDMI concept something – Gobu CSG Jul 07 '22 at 17:45
  • Or are you mentioning ui screen !? If it is screen create intent and startActivity enough – Gobu CSG Jul 07 '22 at 17:46
  • Not foldable phone, it is a HDMI monitor, like this. https://medium.com/@huuphuoc1396/display-your-app-on-multi-screen-at-the-same-time-in-android-88b4c57a81 – CL So Jul 07 '22 at 18:16
  • I don't have experience on that .. I'll check.. If I get something let you know – Gobu CSG Jul 07 '22 at 19:10

0 Answers0