onPause() and onResume() functions while designing the home screen

549 Views Asked by At

I am designing an Home Screen in which I am showing the applist in a gridview. There are some animation in the 1st screen of my HOMESCREEN and when you press on a button then a grid is shown. So, the problem is that; 1)).I opened an application from the gridview,it opens normally but when i exit from that particular application,the BACKButton takes me to the screen of GridView.(It is OKAY). Now,When I press the HOME Button,It is doing the same work as it does on BACKBUTTON.

I am listening to these butons using dispatchKeyEvent(KeyEvent event) but it is not working with home button.. Logically ,I should not work because this dispatchKeyEvent(KeyEvent event) works for my application not for any other application..

I found some other paths to resolve this problem,

When i open other apps,onPause() and onStop() functions are called in my homescreen,suppose I set a flag over there but by doing this How will I be able to know that after doing his stuff in OTher application user presses HOME BUTTON or BACK BUTTON.

2

There are 2 best solutions below

0
On

You should declare your home activity look like below code

 <activity
            android:name="com.android.launcher2.Launcher"
            android:launchMode="singleTask"
            android:clearTaskOnLaunch="true"
            android:stateNotNeeded="true"
            android:theme="@style/Theme"
            android:windowSoftInputMode="stateUnspecified|adjustPan"
            android:configChanges="locale">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.HOME" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.MONKEY"/>
            </intent-filter>
        </activity>

and declare gridactivity as a normal activity in Manifest

I hope you got solution.

0
On

dispatchKeyEvent(KeyEvent event) won't be able to catch Home button click. if you want to capture Home button click use onUserLeaveHint(). And to capture back button specifically use onBackPressed().