问题描述:

When I create a Intent:

Intent in = new Intent(this, myclass.class);

this.startActivity(in);

I create a new Intent but the last Intent is not destroy

It's still in memory

How can I kill this last destroy?

So I want to create two Intents in two different classes:

in the first class I write this:

Intent in1 = new Intent(this, myclass2.class);

this.startActivity(in);

and in second class I write this:

Intent in2 = new Intent(this, myclass1.class);

this.startActivity(in2);

but when I Click in1 will create and the in2 will destroy, and when i Click on in2 the in2 will create and in1 will destroy.

a want to clean my phone memory.

网友答案:

You can prevent the user from going on to the previous Activity by overriding the back key functionality like this:

public void onBackPressed() {
       //doing nothing on pressing Back key
       return;
    }

Though this approach is not encouraged.And as everybody knows, the Activity will be automatically killed when certain aspects come up like memory requirement. You cannot destroy one Activity at your free will.

Though for specific cases like a welcome splash screen maybe, you can do one of the following two things:

1) call finish() method on your current Activity as you move onto your new Activity(generally done when using Thread).

2) use the following in your manifest:

<activity android:name=".WelcomeScreen" android:noHistory="true" ... />

This will tell the device to not keep this Activity on the Activity stack.

网友答案:

Activities remain in memory until Android decides it should be removed. Don't worry about it. It'll get destroyed when more memory is needed. It's effectively "unused" memory until your top activity uses it.

网友答案:

I'm not sure to understand your question... You want to start a new activity and destroy the previous one? If this is what you need, you can use:

startActivity(new Intent(this, myActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));

The "CLEAR_TOP" flag tell "Hey! I want to start this activity and kill all the other ones!"

网友答案:

Since the Android runtime environment uses garbage collection (just like "traditional" Java), you won't need to worry about manually destroying objects in most cases. The memory taken up by the unused Intent object will be recycled if the system needs additional space for new objects.

相关阅读:
Top