Home button Android 3.0, 4.0 - java

I have an application in android that includes a application home page. I override the hardware Home button to go back to the application home page for API level 10 or less.
My application works fine on Android version 3.0 and 4.0. The problem is overriding the Home button. I found a discussion ( Disable Home Button in Android ICS (4.0) ) which includes a method to implement a home screen somehow. It is suggested by #Chalaman.
I did not get the point yet. Is there any one that can help me more by providing some codes?
when we use :
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
in the manifest file, a dialog pops up when we click on the home button. It includes the home phone page icon and application icon. we can make a choice. If we select the application icon, we stay in the application. The problem is we stay also in the same activity. How can I go to another activity (home page of my application)?
We can to it in API level 10 or less:
#Override
public void onAttachedToWindow() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_HOME:
loadStartPage();
break;
}
return super.onKeyDown(keyCode, event);
}
private void loadStartPage() {
Intent intent = new Intent(getApplicationContext(), StartActivity.class);
intent.putExtra("user", user);
intent.putExtra("user_id", user_id);
intent.putExtra("server", server);
intent.putExtra("password", password);
main_activity.startActivity(intent);
}
How to Load Start Page in API level 11 or greater?

The answer is simple: don't do it.
Users expect their apps to be consistent across the entire Android platform. I can't imagine a situation in which you could ever justify overriding the home button.
Edit:
To be clear, there are apps out there that appear to override the home button (i.e. the Car Mode app, which won't allow you to escape the app unless you click the "Exit" button). Apps such as these don't actually explicitly override the "home button". You can read more about it here.

the android home screen is just an app. it is started by a well-defined broadcast intent that is issued when the home key is pressed.
in a nutshell, you implement an android application that listens to the same event that the stock android home screen app listens to. when the user presses home, they will see the chooser allowing them to select from the stock home screen app, our your app. here's what the intent filter would look like,
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
i realize that's probably not what you want. however, there's no way to completely override the home key. this is intentional as it prevents malicious apps from locking you out of the home screen. the home button is sort of the guaranteed to work "get me out of here" button.
your users can flag your app as the default to handle the home broadcast, but that's probably not what you want either because from then on they want be able to get to the stock home screen unless they clear default or uninstall your app.
preventing users from accessing the home button is a pretty nasty thing to do. are you sure that's what you want?

Override onKeydown can't solve your problem .. when you press home button activity calls onPause() .. so put your method inside of onPause().. I guess it will work ..
protected void onPause() {
super.onPause();
loadStartPage();
}
private void loadStartPage() {
Intent intent = new Intent(getApplicationContext(), StartActivity.class);
intent.putExtra("user", user);
intent.putExtra("user_id", user_id);
intent.putExtra("server", server);
intent.putExtra("password", password);
main_activity.startActivity(intent);
}

Related

no apps can perform this action android studio

Im new to this area, I have a mission, I have build up an application that register some data that I have insert.
lets call this app "A", now i copy the same app and called it "B".
In app "A" and "B" in the manifest file I write this:
<activity
android:name=".Register"
android:exported="false">
<intent-filter>
<action android:name="com.action.ex3.register" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Now in app "C" I want to use what app "A" or "B" doing, in order to to that in the app "C" I write this:
public void registerBtnClicked(View view) {
Intent intent = new Intent(); //implicit activity.
intent.setAction("com.action.ex3.register");
startActivityForResult(intent, REGISTER_ID);
}
what I expected that will happen it that when I press in my app "C" on the regiser button a pop out with a "chooser dialog" with option to choose "A" or "B", but instead of that I got "chooser dialog" with "no apps can perform this action android studio"
what im doing wrong?
here a picture of the problem
App C "no apps can perform this action android studio"
Im using pixel emulator in android studio IDE.
Your activities are not exported. They cannot be accessed from other apps. Change android:exported to be true if you want other apps accessing your activities.

Why finish() make all the activities can't go back to previous activity?

I'm new in Android Java and I have some question about the intent. This is the intent concept of the app
First, The Launch Activity have two button to let the user push the screen to Register Activity and Login Activity . Other than that, User also can press the back button to go back the Launch Activity. So it is not include finish().
//Login Activity
Intent i = new Intent(LaunchActivity.this, LoginActivity.class);
LaunchActivity.this.startActivity(i);
In the AndroidManifest, Main Activity (yellow) is the first activity.
<activity
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
On Main Activity, I will check if whether the user is login to the app or not. If haven't login, I will push the screen to Launch Acitivty by using this code. (I'm using this code on the sign out button too)
Intent i = new Intent(MainActivity.this, LaunchActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i);
finish();
So I use finish() to prevent the user go back to the Main Activity.
But the problem is the finish() also let the Login and Register Activity can't go back to the Launch Activity.
So is the finish() make all the activities can't go back to previous activity? why and how to fix it?
Due to the comments, I know the problem is the flag. So I removed those flags when checking the login status and sign out. but after I login successfully (login screen to main screen), I can press the back button to go back to the launch screen on the main screen. why? It only happen when first login. after I relaunch the app, the app work fine. This is the code I use on the login button.
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i);
finish();
Is it because we can't destroy the launch screen on the login screen? but adding those flags should be destroy all the history right?

Start the First screen each time app comes from background in android

I have an app that has 5 activities (A,B,C,D,E). The app can navigate between those activities. When the user presses home button in device the app goes background and after when the app comes to foreground, first activity should be launched i.e A activity.
Example: app in D activity, after pressing home, the app goes background and when it comes to foreground again it should open A activity not D.
Solutions which i have tried is launch mode, I set the launch mode for A activity (singleInstance) but could not able to find the required solution.
For launch same Activty you Should clear all the Activity when app goes into background.When app goes background use below code that will clear current activity and all other activity that are in stack.
For API 16+, use
finishAffinity();
For lower (Android 4.1 lower), use
ActivityCompat.finishAffinity(YourActivity.this);
When you press Home-Button change to Activity A. Maybe this will work:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_HOME)
{
Intent intent = new Intent(this, MyActivityName.class);
//replace MyActivityName.class with the name of your Activity A
startActivity(intent);
}
return super.onKeyDown(keyCode, event);
}
you may get ondestroy() or onpause() method. on it youcan do
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish(); //
You can set android:clearTaskOnLaunch="true" for Activity A in your manifest file to achieve what you want:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.beispieldomain.stackoverflowxmlparse">
<application
...
<activity
android:name=".MainActivity"
android:clearTaskOnLaunch="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Have a look here:
Managing Tasks
clearTaskOnLaunch

Android Manifest- intent filter and activity

Could someone explain the following lines in the manifest -
<activity
android:name=".AboutUs"
android:label="#string/app_name">
<intent-filter >
<action android:name="com.example.app1.ABOUT" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
How are the fields in activity and intent filter important and when are they used/referred ?
Sorry, i tried to read the documentation but still couldnt figure much out.
Thank you
android:name=".AboutUs"
This is the name of your Activity class, the dot at the front is shorthand notation for your package. So this actually stands for com.your.package.name.AboutUs which means your java file that represents this Activity is called AboutUs.java
android:label="#string/app_name"
label is the string that gets shown in the launcher(if the activity is listed in the launcher) and at the top of the window when the activity is open.
<intent-filter > ... </intent-filter>
intent filter defines the Intents that your activity "listens for" in order to launch.
<action android:name="com.example.app1.ABOUT" />
<category android:name="android.intent.category.DEFAULT"/>
Action and category are both fields that get set on an Intent before it is "fired off" into the system. The system will then look for any activities that match both the action and category and if it finds one then it will launch that activity, or if it finds multiple it will show the user all of them and let them pick.
In your case your the action you are listening for com.example.app1.ABOUT is a custom action that is specific to your app, not one of the systems actions.
So here is what an intent that would start this particular activity might look like:
Intent i = new Intent();
i.setAction("com.example.app1.ABOUT");
i.addCategory("android.intent.category.DEFAULT");
startActivity(i);
Note that because you've created a custom action, this intent does not require access to your AboutUs.class so this intent could technically be fired from any app on the device and it would launch into your activity.
For the ACTION you have this answer and for the DEFAULT category you have this answer
An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. To declare your activity, open your manifest file and add an element as a child of the element like your example.
The android:name attribute is the only required attribute—it specifies the class name of the activity.
The android:label attribute is a user-readable label for the application as a whole, and a default label for each of the application's components
An element can also specify various intent filters—using the element—in order to declare how other application components may activate it.
The element specifies that this is the "main" entry point to the application.
The element specifies that this activity should be listed in the system's application launcher (to allow users to launch this activity).
Please refer to the http://developer.android.com/guide/components/activities.html

Restore android app stack from background

Lets say I launch my app from the home screen, navigate through some activities, then I press the home key and do something else in the Gmail app.
After i'm done checking my mail,I press the home key again to leave the Gmail app and click my app's icon at the home screen again to return to it.
When I return to my app, I want it to return to the last activity I was at, NOT start a whole new session. I've been trying to figure this out all day.
My manifest for my first activity is as follows:
<activity android:name=".Main"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:alwaysRetainTaskState="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The category attribute LAUNCHER makes my app ALWAYS start at activity Main, so I don't know how to go about restoring the last activity. People have told me to use sharedpreferences to save the last activity and load it on Launch, but I don't think it's intended to be done that way because it isn't very elegant.
I think its the only way because what happens when you're launching an app is that Launcher application sends intent "android.intent.action.MAIN" And the only activity in your app that responds to this intent is your main activity, thus it gets activated. So the only thing you can do is save somewhere your session and on activity start up if there's already saved session restore the app to the previous state.
Try using one of these in your manifest :
<activity android:launchMode=["multiple" | "singleTop" |
"singleTask" | "singleInstance"] ...
Are onResume() and onPause implemented properly?
protected void onResume(){
super.onResume();
}
protected void onPause() {
super.onPause();
}

Categories

Resources