Go back to previous activity

1k Views Asked by At

I have a page product to which overtime u press a button a different id is sent through intent.There is a buy button in this page if we are not logged in it will take us to login page so after I login how to I go back to this page with the same id that was passed to this intent.

2

There are 2 best solutions below

1
On

Start the login activity using startActivityForResult() method.

When your work is done in login activity then call setResult(RESULT_OK) and call finish() to finish that activity.

You will get back to your previous activity. There you have to override the onActivityResult() method if you want to do something.

1
On

Suppose If you are clicking on Button click event

Button button=new Button(getApplicationContext());
    button.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent intent=new Intent(getApplicationContext(),LoginActivity.class);
            intent.putExtra("yourid","123");
            intent.putExtra("goingtologin","true");
            startActivity(intent);
        }
    });
    if(getIntent().getStringExtra("yourid")!=null)
    {
        String id=getIntent().getStringExtra("yourid");
    }


    //in your login activity

    String iscomingfromlogin="";
    String id="";
    if(getIntent().getStringExtra("goingtologin")!=null)
    {
        iscomingfromlogin=getIntent().getStringExtra("goingtologin");
    }
    id=getIntent().getStringExtra("yourid");

    // after login successfully check


    if(iscomingfromlogin.equals("true"))
    {
        Intent intent=new Intent(getApplicationContext(),YOURPREVIOUSACTIVITY.class);
        intent.putExtra("yourid",id);
        startActivity(intent);
        finish();
    }