Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity not closing on Back press when theme is Theme.NoDisplay

I am setting Activity Theme to Theme.NoDisplay but When it open but on press back button Activity not closing/destroying. It should close/destroy on back press.

Guys help me why this is so and any solution to resolve this.

public class MainActivity extends Activity {

    // Tag of the Activity
    private static String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DatabaseManager.init(this);

        NFCIItem mNFCItem = new NFCIItem();
        mNFCItem.setSerialNumber(1);

        DatabaseManager.getInstance().addWishList(mNFCItem);
        final List<NFCIItem> wishLists = DatabaseManager.getInstance().getAllNFCSerialNumber();
        Log.v(TAG, wishLists.toString());

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

AndroidManifest.xml

<activity
    android:name="com.example.appdemo.MainActivity"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoDisplay" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
like image 611
N Sharma Avatar asked Jan 27 '14 12:01

N Sharma


1 Answers

I imagine it is closing but you see no evidence of this because it is 'NoDisplay'. An Activity using this theme has no visible UI (hence it is not logical for such an Activity to process UI events) and should not be kept alive. See this question for example:

how to completely get rid of an activity's GUI (avoid a black screen)

If you do want this Activity's layout to be visible and to handle events, you should use a different theme. If on the otherhand this is an invisible Activity that just does some background processing, call finish() in onCreate to close it when that processing is done.

like image 99
NigelK Avatar answered Oct 05 '22 20:10

NigelK