Exception in one activity doesn't stop other activities - android

My OnCreate method (in MainActivity):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, DetailsActivity.class);
startActivity(intent);
// This will throw exception
Integer.parseInt("Güzelim");
}
});
}
When a button is clicked, the app crashes; showing the dialog "Unfortunately app has stopped". When I click OK button, the DetailsActivity screen is displayed. Why does this happen? I would like the entire app to crash.

#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// This will throw exception
Integer.parseInt("Güzelim");
Intent intent = new Intent(MainActivity.this, DetailsActivity.class);
startActivity(intent);
}
});
}
This should crash your whole app.

App is crashing because you are trying to convert String into Integer which is throwing NumberFormatException.

This answer to a similar question might help
For example, a user might be browsing the web, click a youtube link
launching the youtube app, then click a share button to launch their
favorite social networking app and post the video link. This is all
part of the same task; if the user presses back several times they'll
come back to the page in the browser that they started from.
Once you start up the social networking app, the system might decide
that it's running low on memory and it's going to kill off the
browser's process to free up more. (After all, it's not in front and
the user won't notice.) When the user presses the back button to
return to the browser Activity it gets restarted and reconstructs the
last state where the user left it. At worst the user experiences a
short delay while things reinitialize.
But this same sequence of events restoring a previous Activity state
can happen even within the same app in the same process. In your
scenario, Activity B closed as the result of the crash. So the system
does exactly what it always does - it returns to the previous
Activity: Activity A. But Activity A's process isn't still around (it
crashed!) so the system restarts it and it can reconstruct its
previous state.
Crux of discussion: As a philosophy Android tries to give a feel that all the apps are running all the time, as part of this philosophy it tries to restore from neatest point possible.

Related

How to switch to a previous activity in Android without error message?

I have 2 screens corresponding to 2 activities on my Android app. I am trying to go back to the first activity from the second screen by clicking a Back button. Even though my first activity restarts, I get an annoying pop-up message "Unfortunately the app has stopped" as my app refreshes to go back to the first activity. I would like to not have this message appear.
I have tried different variations of code and the message appears each time :
#Override
public void onBackPressed() {
startActivity(new Intent(this, MainActivity.class));
}
or
public void finishActivity(DisplayMessageActivity v){
Intent intent = new Intent( this, MainActivity.class );
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
or even just
public void finishActivity(DisplayMessageActivity v){
finish();
}
You should read the logs to find out what the exception is and why it is occurring. Places to check:
If back navigation logic is inside an OnClickListener, something inside the OnCLickListener code is probably null.
The activity it should go to after pressing back is having a null value for something you're trying to use.
Any asynchronous calls from old activity finishing after it has been removed.
Your log should tell you exactly what it is.

Android's recent apps list redirecting to another app

I have code in an App (let'call it app 'A') that starts an activity on a second app (let's call it app 'B') when the user clicks a button.
So far so good but I'd expect to later be able go back to app 'A' using the system's overview screen (aka recent apps list), but when I try to I'm redirected back to App B automatically, as if I was clicking the button a second time.
The phone is running Android KitKat and the code looks like this:
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
startActivity(new Intent("com.some.package.MAIN"));
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
});
So far so good but I'd expect to later be able go back to app 'A' using the system's overview screen (aka recent apps list), but when I try to I'm redirected back to App B automatically, as if I was clicking the button a second time.
This is the default behaviour of Android.
Android Activity life cycle uses a context based statck called Task.
So the App 'B' is simply inside the the Task of calling Activity App 'A'.
You can override this behaviour by using the following flag.
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
Intent i=new Intent("com.some.package.MAIN");
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
});
1) FLAG_ACTIVITY_NEW_TASK - If set, this activity will become the start of a new task on this history stack. A task (from the activity that started it to the next task activity) defines an atomic group of activities that the user can move to. Tasks can be moved to the foreground and background; all of the activities inside of a particular task always remain in the same order.
2) FLAG_ACTIVITY_CLEAR_TOP - If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
3) FLAG_ACTIVITY_SINGLE_TOP - If set, the activity will not be launched if it is already running at the top of the history stack.

Back button behaviour issue

Helping to create an app, very new to using android studio and as a programmer. Just wondering if anyone could help with this current issue.
This is a secure server communication app.
On the app there is a back button on the conversation screen which should take you back to the conversation thread. Once you kill the app on your phone and open a message on the app through a notification it takes you directly to the conversation screen, but as you hit the back button instead of going back to the conversation thread it takes you off the app completely which is not what we want.
Can someone please tell me whereabouts I should go to fix this problem and the code to do so.
Thank you
It because once you open activity from the notification will be root activity. and hitting back button just close the app.
You need to check for the root activity. Try this method isTaskRoot().
Once you open activity initial check for that the activity is root or not. If it is root then call startActivity() else if not root call back functionality.
So for example:
YourBackButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(isTaskRoot()){ //Check if it is your root activity
Intent intent = new Intent(Current_class.this,Conversation_thread.class);
startActivity(intent);
finish();
}
else{
finish();
}
}
});
public void onBackPressed() {
//start your conversation thread class here. something like this
Intent intent=new Intetn(Current_class.this,Conversation_thread.class);
startActivity(intent);
}
Hope this helps you!.

Pressing HOME button does not return widget ID from configuration activity

Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \widget configuration activity.
public class WidgetConfigure extends Activity {
private int widgetID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("Lifecycle", "WidgetConfigure.onCreate");
setContentView(R.layout.widget_configure);
widgetID = this.getIntent().getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
setResult(RESULT_CANCELED, new Intent().putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID));
}
public void onButtonClicked(View view) {
Log.v("Lifecycle", "WidgetConfigure.onButtonClicked");
if (((RadioButton) this.findViewById(RefreshStrategy.SYSTEM)).isChecked()) {
WidgetProvider.configure(widgetID, true);
} else if (((RadioButton) this.findViewById(RefreshStrategy.USER)).isChecked()) {
WidgetProvider.configure(widgetID, false);
}
setResult(RESULT_OK, new Intent().putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID));
finish();
}
}
In theory RESULT_OK should be sent after pressing button in activity and RESULT_CANCELED in any other case, but it does not happen when physical button HOME on device is pressed.
Any ideas how to fix this issue?
This is not an issue in your code - this is normal, standard Android behaviour. (Maybe a bug?)
I cannot find it documented, but if you try to add Google's widgets that require configuration, and then press HOME, you see that the widgets do not get added. We can think of the HOME key as a CANCEL button in a way.
I spent most of today thinking my own code had a bug in it, and looking for a way to fix it.
If anything, I believe this could be a bug in the framework, because the result is not delivered at all. In addition, I get a similar failure if I launch a new Activity from the config activity and then exit from there.
FWIW I get that error message on my Galaxy Nexus 4.2.1; but on the Nexus 7 4.4.2 the error is not shown, but the behaviour is the same.
Update: According to Dianne Hackborn, this is expected behaviour, at least from the user's point of view:
The normal experience for an app widget that is going to provide a
configuration UI is that pressing back or home from that will cancel
the addition.
App Widget Configuration Activity does not behave like Activity
So as a developer this may seem strange, but as a user it all works out fine in the end.

Onclick back button the application stops

I have one problem regarding my app,when i click back button my application is getting finish.i need to prevent it.How could i make it .
#Override
public void onBackPressed() {
Log.d("CDA", "onBackPressed Called");
Intent setIntent = new Intent(Intent.ACTION_MAIN);
startActivity(setIntent);
}
I have tried something like this.But its not working .Actually what i need is when i will click back button ,the application should not finish it should run in background.can anybody help me out.#Thanks
Try doing it like this:
#Override
public void onBackPressed() {
Log.d("CDA", "onBackPressed Called");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
By doing this, your activity will not be destroyed (i.e. onDestroy will not be raised). But also, there's no guarantee that Android will preserve your activity for long.
In case, you are running a process that you want to keep on running even in background, then I would suggest you to go for Service or IntentService.
What do you mean by "it should run in background"? Why would you want that? If the user wants to keep the app opened, he can use the home button and the app won't be closed, if he presses the back button he wants to close the app. If want to have something that is still running even after the user closes the application you should take a look at Service http://developer.android.com/reference/android/app/Service.html, this will continue running even after the user closes the app

Categories

Resources