msg's executing time is too long in Android Java - 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);
});

Related

Android back button forcing app restart and not resuming properly

In my app my first activity to launch is a login activity (A). When the login is successful another activity is launched (B), in doing so activity A is killed using finish(). This is to prevent the user being taken back to the login screen if they hit the back button, which works fine. Now when the app is closed from activity B using the home button and restored from the multitasking view the user comes back to activity B, which is great. However, when the user taps the back button in activity B the app closes and when the app is restored from the multitasking view, activity A is launched again when I actually want the behaviour clicking the home button gives and presenting the user with activity B.
Is there any way to do this?
You should simply add a check to your login activity, if the user is already signed in finish it and launch your B activity.
I'm really silly, just found my answer in one of the 'related' questions but it didn't come up when I created my question, oh well.
Here's what I did:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() == 0) {
Log.d("CDA", "onKeyDown Called");
onBackPressed();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onBackPressed() {
Log.d("CDA", "onBackPressed Called");
Intent setIntent = new Intent(Intent.ACTION_MAIN);
setIntent.addCategory(Intent.CATEGORY_HOME);
setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(setIntent);
}
This essentially emulates what the home button would do in activity B.

Making an activity which started itself go back to previous screen with "onBackPressed()"

I have an activity which is repeated "x" times with this piece of code :
//create intent to start itself again with different parameters
Intent intent = getIntent();
intent.putExtra(CATEGORY_ID, temp.getCat_ID());
intent.putExtra(CATEGORY_SHOPID, temp.getShopID());
intent.putExtra(CATEGORY_SITEID, temp.getSiteID());
intent.putExtra(CATEGORY_NAME, temp.getCat_Name());
finish();
startActivity(intent);
My goal is that when the user presses the back button that the same activity gets started as there was before with the same parameters as before and such. I tried saving them in an Activity ArrayList but that doesn't seem to work.
When you call finish, your activity will end.
When you don't call finish, your activity just go to the background. In that case, when you press the backbutton in your new activity, your previous activity comes up.
I think when you just delete finish(), it will work.
It's okay if you save the parameters you've already set earlier, but you need to retrieve all that information from the Intent itself and repopulate your activity with the proper data.
This must be done like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String categoryId = getIntent().getStringExtra(CATEGORY_ID);
// retrieve the others and populate your activity
}
in your activity, get event back
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
// and you have to make a intent to call this activity again with parameter other
finish(); // and call finish() here to close the old activity
}
return super.onKeyDown(keyCode, event);
}
hope this help!
Have you tried overriding the onBackPressed method?

Check if back key was pressed in android?

Say I'm on my main activity and I start a new activity
MainActivity > NewActivity
And from NewActivity I press the back key
MainActivity < NewActivity
I want MainActivity to do something if it's being displayed after NewActivity is closed, but not when MainActivity is run normally, such as when first running the application. Does anyone know if this is possible?
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.d(this.getClass().getName(), "back button pressed");
}
return super.onKeyDown(keyCode, event);
}
#Update. If you want to be notified when NewActivity is finished, you have to start it by startActivityForResult(Intent, requestCode). Then, you must override onActivityResult() on MainActivity. Check the requestcode parameter here, if the return code equals the submit code (when you start childActivity), put some code to do your business.
int MYCODE=1000;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Result OK.d.
if (requestCode == MYCODE) {
// do something good
}
}
I try the below method to detect the back button pressed on the action bar in activity by the first method and the second one is used to detecting the mobile hardware button back or kill activity button.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed() {
setResult(RESULT_CANCELED);
super.onBackPressed();
}
You can override onBackPressed() method in NewActivity which will detect when back button is pressed. And then to inform the MainActivity about it, you can send a boolean flag in a bundle so that MainActivity detects that its opening after NewActivity.
In NewActivity:
#Override
public void onBackPressed() {
boolean fromNewActivity=true;
Intent mainIntent = new Intent(view.getContext(), MainActivity.class);
Bundle bundleObj = new Bundle();
bundleObj.putString("fromNewActivity", Boolean.toString(fromNewActivity));
mainIntent.putExtras(bundleObj);
startActivityForResult(mainIntent, 0);
}
In MainActivity in onCreate() method :
Bundle extras = getIntent().getExtras();
boolean fromNewActivity =Boolean.parseBoolean( extras.getString("fromNewActivity"));
Now you can check if the MainActivity is opened after NewActivity or not.
A couple of ideas:
You can just set a flag in MainActivity when it fires up NewActivity.
You can call startActivityForResult from MainActivity and arrange for NewActivity to set a result, which you will receive in MainActivity.onActivityResult() when NewActivity finishes.
When you start NewActivity you need to use startActivityForResult and use a valid requestId. Such requestId will be passed back to you to onActivityResult once NewActivity finishes.

Android : startActivityForResult() with BACK button functionality

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().

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