public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialize UI components TextView textView = findViewById(R.id.textview); Button button = findViewById(R.id.button); // Set up button click listener button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Do something when button is clicked textView.setText("Button clicked!"); } }); } }
public class LoginActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Check if user is already logged in if (isLoggedIn()) { startMainActivity(); } else { // Display login UI setContentView(R.layout.activity_login); } } private boolean isLoggedIn() { // Check if user is logged in // ... return false; } private void startMainActivity() { Intent intent = new Intent(this, MainActivity.class); startActivity(intent); finish(); } }This example checks if the user is already logged in by calling the `isLoggedIn()` method. If the user is already logged in, it starts the main activity using an Intent and finishes the login activity. If the user is not logged in, it displays the login UI using the `setContentView()` method. Package library: `androidx.appcompat.app`.