Android : startActivityForResult() with BACK button functionality - java

I would like to start a new activity for a result, with startActvityForResult(), but I would like to have the back button working as normal in the new activity.
Currently when I invoke a new Activity for result, nothing happens when I press the back button in the new Activity.
I tried something like this:
#Override
public void onBackPressed() {
setResult(0);
super.onBackPressed();
finish();
}
in the new Activity, but it didn't work. Still nothing happens when the back button is pressed.
Is there a way around this?
EDIT : I could of course load the last Activity in the onBackPressed() (can I?), but it seems like a rather crappy hack.
Alex Ady's answer solves my problem, but I still don't understand why onBackPressed() doesn't work. The working code now is something like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
setResult(1);
finish();
}
return super.onKeyDown(keyCode, event);
}
I could use an explanation.

You could try
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
finish();
}
return super.onKeyDown(keyCode, event);
}

You shouldn't have to override the Back button behavior at all. By default, if the user presses the back button, the result will be Activity.RESULT_CANCELED.

Try getting rid of the line that contains the finish().

Related

msg's executing time is too long in Android Java

I know this is common to ask but I have trouble using super.onBackPressed(); that it crash my app eventually, So let's say I'm in the second activity and I want to go back to the mainActivity.The problem is when I go back the logcat says msg's executing time is too long and sometimes it crashes the app, can someone have an idea for this?
second activity goes back to second activity
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
super.onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
first activity goes to 2nd activity : I didn't finish() the intent because I use backPressed on second activity
InventoryList.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, com.example.cashgrantsmobile.Inventory.InventoryList.class);
startActivity(intent);
});

Remap physical back button for "select_button" in android tv webvew application with java

I'm a french developer , i'm creating an android tv application about cloud Gaming in a webview with java.
My application start a gaming stream direcly in the webview, on fullscreen, the physical buttons of controlers are working with the game, eccept the "view button" the "back button'.
This "view button" or if your prefer "select button" is for android tv a back button to the homescreen. So i have to overiding this back button, and i want replace it by a "select button" that it can interact with the games for displaying maps and inventory like in rpg games.
I know that the name will be "button_select" for interact with the pc game. So in android tv for now i will always redirected to the home page.
this is a sample of my code.
public class MainActivity extends AppCompatActivity {
#Override
public void onBackPressed() {
return;
}
With this overide, the back button is completly disabled for now, i want replace it or call the "button_select". I readed something about "handler" perhaps this is the solution.
Edit 16/06/2021
I tested much coded, but nothing work.
this one
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
keyCode = KeyEvent.KEYCODE_BUTTON_SELECT;
}
return super.onKeyDown(keyCode, event);
}
And its variants
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (action == KeyEvent.ACTION_DOWN ){
//Do something in the back button
keyCode = KeyEvent.KEYCODE_BUTTON_SELECT;
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
In game, the back button is disabled but, the" keycode_button_select "not interact with the game. There is nothing. I test with the "keycode button_start" the same things.
I tested the app button mapper for android tv, there is an option for custom keycodes with adb, and nothing is working in game.
https://play.google.com/store/apps/details?id=flar2.homebutton&hl=fr&gl=US
So by the code or by an app, nothing is working for now, i don't want to forcing the "root mode" for this things. Perhaps i'll just have to implementing a gamepad Plugin in java. I don't know...
Edit 18/06/21
I can capture the "back button" and display a dialog alert that appear on middle of the screen. But the actions after has no effect for now. I tested "dispatchEvent" with no success for now. I will testing the functions "robots". Perhaps "robot" and "dispatchEvent" will working together.
Thank you for your help.
I am assuming that you want to call button_select key when user presses back button.
In this case, create a method to call upon pressing back button. And call that method from the onBackPressed method.
For example:
public void callButtonSelectKey() {
//This could be done in either ways.
//1.
val i = Instrumentation()
i.sendKeyDownUpSync(KeyEvent.KEYCODE_BUTTON_SELECT)
//2.
val keyEventDown = KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BUTTON_SELECT)
val keyEventUp = KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BUTTON_SELECT)
dispatchKeyEvent(keyEventDown)
dispatchKeyEvent(keyEventUp)
}
#Override
public void onBackPressed() {
methodToCallUponBackPress();
return;
}
Now when the user presses the back button, it will call callButtonSelectKey method invoke "button_select" key. (i.e. remapped backbutton to button_select)
Let me know if you have any more questions.
EDIT
In an Object Oriented Language, overriding the class's (in this case KeyEvent) field (in this case, keycode) will do nothing. Like the code below:
//Do something in the back button
keyCode = KeyEvent.KEYCODE_BUTTON_SELECT;
To call button_select when you receive the event of KEYCODE_BACK, you should manually invoke the method to call Key Event, in this case the method I gave you earlier dispatchKeyEvent .
So you should make your change into something like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
//This could be done in either ways.
//1.
val i = Instrumentation()
i.sendKeyDownUpSync(KeyEvent.KEYCODE_BUTTON_SELECT)
//2.
val keyEventDown = KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BUTTON_SELECT)
val keyEventUp = KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BUTTON_SELECT)
dispatchKeyEvent(keyEventDown)
dispatchKeyEvent(keyEventUp)
}
return super.onKeyDown(keyCode, event); //Edit this return statement if you want to ignore a keypress.
}

Handling the Back Button

I am working on the following code:
private class HandleBackButton implements OnKeyListener
{
#Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
// TODO Auto-generated method stub
if(arg1==KeyEvent.KEYCODE_BACK)
{
showResults(0);
}
return true;
}
}
I am somewhat new to android and my purpose is to operate the above code when the back button is clicked. User can click the back button any time. But, how can I set this listener to the Activity? I can't find something like this.setOnKeyListener().
I am using Android 2.3.3.
For the Activity you should override onBackPressed which is invoked when you press the back button. OnKeyListener dispatches key events to the view. You find setOnKeyListener defined in the View class
Interface definition for a callback to be invoked when a hardware key
event is dispatched to this view. The callback will be invoked before
the key event is given to the view. This is only useful for hardware
keyboards; a software input method has no obligation to trigger this
listener.
Just override the onKeyDown() method of Activity.
You don't have to set a listener then.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK)
{
showResults(0);
return true;
}
return super.onKeyDown(keyCode, event);
}
Optionally you can also override onBackPressed() if your api level is >= 5.
You can use onBackPressed():
#Override
public void onBackPressed() {
showResults(0);
}

Button return = action button home

I'm doing an Android app and I would know if this was possible:
When the user press the return button, it does the same action as the home button.
Following the question I linked: Override back button to act like home button:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
Should be the solution.
This overrides the backbutton, and puts the task to the back. Which emulates hiding like the home button. Read the other question for more details, and solutions.

How can I use the Android back button to move back within my app instead of closing my app?

My app has three activities, A, B and C. I am moving from A to B through an OK button, and I want to move back from B to A by using the default back button of Android devices. When I press the button, though, the entire app gets closed. How can I get around this problem?
I suspect you call finish() in your OK button onclick listener. Don't do that. finish() removes your activity from activity stack.
Read more here.
why start your activity for result ? when you press the backbutton, the result is set to RESULT_CANCELED form the B activity, so it crashes if you don't handle the resultcode...
you can handle the backpress like this
private static final int NONE = -1;
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
setResult(NONE, intent);
finish();
return super.onKeyDown(keyCode, event);
}
When you are Ok button r u starting an intent...like
Intent int=new intent(context,B.class);
startActivity(int);
then if you are not handling backbutton.
If use default back button...it will goes back to A.
Hope it helps...
In my onClick method (in Main Activity) I use the code:
Intent intent = new Intent(context, SecondActivity.class);
context.startActivityForResult(intent, SecondActivity.SECONDACTIVITY_REQUEST);
In the manifest I've got:
<activity android:name=".SecondActivity" android:screenOrientation="landscape" android:launchMode="standard"></activity>
This works for me without any other settings that I can see. What events are you responding to?
Note that you can also go back an activity, in code like this:
super.setResult(Activity.RESULT_OK);
super.finish();
Edit...
Make sure you're not swallowing the event in the Main Activitys onKeyDown event.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//your code here
//if (keyCode ==
//...
//else
return super.onKeyDown(keyCode, event);
}

Categories