-1

I want to execute the login_activity first, then the form_activity like this.

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // Check Login
    val intent = Intent(this, LoginActivity::class.java)
    startActivity(intent)

    // After login, call FormActivity
    val intent2 = Intent(this, FormActivity::class.java)
    startActivity(intent2)

}

My problem is that if I run this, the app directly opens FormActivity, however, if I comment out the call to FormActivity, then the LoginActivity opens successfully.

What I need is to open the FormActivity only after the LoginActivity returns successfully.

Karl
  • 481
  • 1
  • 6
  • 13

2 Answers2

1

You need to startActivityForResult . Read here : https://developer.android.com/training/basics/intents/result.html

A very good example can be found here: How to manage `startActivityForResult` on Android?

1

You should use startActivityForResult(). Compared to startActivity(), startActivityForResult takes an additional integer argument as a request code which identifies your request to the activity. You'll also need to over-ride the onActivityResult() method to handle the called Activity finishing

Example of starting an activity for a result:

static final int MY_REQUEST_CODE = 1;

public void foo(){
    Intent someIntent = new Intent(YOUR_INTENT_HERE); 
    startActivityForResult(someIntent, MY_REQUEST_CODE);
}

Handle the result

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if(requestCode == MY_REQUEST_CODE){
        bar();
    }
}

You use the request code as well as the Intent called data to handle whatever data results from launching the other Activity

In Kotlin:

companion object{
    private const val MY_REQUEST_CODE = 1;
}

fun foo(){
    val someIntent = Intent(YOUR_INTENT_HERE)
    startActivityForResult(someIntent, MY_RESULT_CODE)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?){
    if (requestCode == MY_REQUEST_CODE){
        bar()
    }
}

For more info: https://developer.android.com/training/basics/intents/result.html#ReceiveResult

Christopher
  • 156
  • 1
  • 3