简体   繁体   中英

How to finish Activity [A] from Activity [B] without Starting new Activity[B]?

I have My first Activity - [A] and my Second Activity [B]. I Starting my transparent-[B] Activity from Activity [A] in my PreExecute of BackGroundTask using

Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);

and from my PostExecute i want to close my transparent Activity [B] to continue work with Activity [A] like:

Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

but it give's me Error cause i haven't an correct Intent.

i can do like:

Intent intent = new Intent(getAplicationContext(), ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

This code will create a new Activity [A]. But i need to continue my work with Activity[A] not to create a new Activity [A]. How can i finish() Activity[B] from Activity[A] without errors, go back to Activity[A] and continue my work.

private class BackGroundTask extends AsyncTask<Void, Void, Void>
{


    @Override
    protected void onPreExecute()
    {
        StartPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params)
    {
        //sending request to server and waiting response here
        return null;
    }

    protected void onPostExecute(Void result)
    {
        super.onPostExecute(result);
        StartPostExecute();
    }


}

protected Void doInBackground(Void... params)
{
    return null;
}

private void StartPreExecute()
{
    Intent intent = new Intent(SigninActivity.this, LoadingLayout.class);
    startActivity(intent);
}

private void StartPostExecute()
{
    Intent intent = new Intent(getAplicationContext(), SigninActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_);
    startActivity(intent);
}

If you say Actvity B should be transparent, I would recommend to use Service. Services are somehow similar to Activities but they run on the background and are not visible to the user. As soon the Service is completed with the tasks, it finish() itself later on. You don't need to leave the Activity A. Is this maybe what you need?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM