Button to go back to MainActivity

I want to create a button that would lead the user straight back to main activity which doesn't have the android name="com.example.example". It has android.intent.etc... How can I reference my button to go back to this activity?

Answers


Lets say your main activity is called Main.java.

btnBack.setOnClickListener(new OnClickListener(){

  private void onClick(){
    Intent intent = new Intent(currentActivity.this, Main.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);   
    startActivity(intent);
  }
});

use startActivity(intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));


Sometimes you can just call activity.finish() to end current activity, so the main (first created) activity will pop out.

If this is not your case, do this:

Intent intent = new Intent(getApplicationContext(), Main.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)             
startActivity(intent);

Intent intent = new Intent(this, Main.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);

Well from wherever you are just call startActivity() with the required parameters inside the buttons onClick method. That's it.


public void onBackPressed(){
    finish();
}

Need Your Help

Inserting new row in sql database table

c# .net sql insert

I have textBoxes in my application. The data entered in those textBoxes are to be inserted in the database. The commandString accepts string type only. So, how can I implement the insert statement?