crash new android process stops entire application - android

I have a android application, which has mainctivty and it starts other activity "secondactivity", this secondactivity I am starting in a new process by adding android:process=":SecondProcess" in manifestfile.
I am starting second activity using below code.
Intent ssIntent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(ssIntent);
below is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test">
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.test.SecondActivity"
android:process=":SecondProcess"
android:label="#string/title_activity_basic"
android:theme="#style/AppTheme.NoActionBar">
</activity>
</application>
There is crash(I added for testing) in the Secondactivity, now as per my understanding only secondprocess Should gets killed whenever crash happens in secondactivity as its running in separate process , but in my test entire android application gets stopped .
Could you please let me know is there a way to handle such a way that only second process should killed and it should not disturb the complete application.
I can handle with try catch, but my goal is to make sure first Activity will be still alive even after second one crashes..

User try/catch to handle the error in SecondActivity...so that it won't crash the whole app.
try {
//Code that creates error in SecondActivity
}catch(Exception e) {
//Close the activity after exception
finish();
}

Related

Android onCreate not firing on new activity

Hi there I am trying to execute some code when I change to the next activity, but it does not seem to work. The Previous activity is a login page if the user is already logged in it goes straight to the new activity. But the onCreate does not seem to fire.
Main Activity
public class MainActivity extends AppCompatActivity {
private View mMainView;
private Meteor mMeteor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("ACTIVITY");
Log.d("SimpleActivity","OnCreate Started");
if(MeteorSingleton.getInstance().isConnected()){
Log.d("Connection", "Connected");
}else{
Log.d("Connection", "Not Connected");
}
}
........
The strange thing is the setTitle works but none of the logs.
Here is some code from the previous login in page.
#Override
public void onConnect(boolean signedInAutomatically) {
Log.i("Connection", "Connected to host server");
if (mMeteor.isLoggedIn()) {
openMainScreen(mLoginFormView);
}
}
public void openMainScreen(View view) {
Intent dashboard = new Intent(getApplicationContext(), MainActivity.class);
startActivity(dashboard);
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="system.carproject.adam.ams">
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".LoginActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:label="Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps"></activity>
</application>
</manifest>
If someone could educate me on this would be great. Can seem to figure it out.
Thanks
You have created two LAUNCHER Activities and that's created two App Icon in your Device so if you think its open directly MainActvity then its possible if you click on second app icon in your device for same application . check your device .
first remove LAUNCHER mode from MainActvity in Android Manifest and then you have to add manual check in your login Activity onCreate() for login status and then startActvity() MainActivty if login status is true.
try removing the intent filter tag from activity tag of MainActivity in Manifest:-
<activity android:name=".MainActivity"
android:label="Activity"> </activity>

Android internet permissions for second activity

I have been trying to download some stuff from the internet in my app, but in the second activity. I have added the permission request, and it works in my first activity, but not in the second one? is there anything i'm forgetting? otherwise, why would i possibly be getting a permission error?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ruslan.seriesstuff">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".Main2Activity">
</activity>
</application>
The problem occurred because of the newer version of android, in my case 6.0. I solved this problem by doing the following. Long press on the app icon in your phone, drag it to the appeared icon of app info. There go to the permissions of the app and make sure you have actually provided the permission to the internet. That's it!

Service in a global process in not getting created

I am creating a service and there will be multiple applications talking to the service. So I want the service to run in a global process i.e. process is not private to any application.
Here are the contents of my manifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.username.servicedemo" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.example.username.servicedemo.FirstService"
android:process="com.example.username.servicedemo.remote"
android:enabled="true"
android:exported="true" >
</service>
</application>
the process attribute under service will create a global process.
the problem is that when I try to start this service from activity on a button click then onCreate method in service in not getting called. Here is the code that executes on button click
public void startService (View view) {
int id = android.os.Process.myPid();
Log.i("MYTAG", " process id activity" + Integer.toString(id)
);
Intent intent = new Intent(this,FirstService.class);
intent.putExtra("key","1234");
ComponentName componentName = this.startService(intent);
Log.i("MYTAG",componentName.toString());
}
What am I missing here? Thanks!
It was a stupid mistake. The service was getting created properly. I was checking the log in onCreate method but the logs were filtered to be shown only from selected application. Since the service was running in a separate process the logs from service were not appearing in the logcat.
I just changed needed to change the filter. Putting a screenshot here in case it helps anyone.

Android app always opens the first activity on resuming

I'm working on an application that currently has two activites, a log in activity and a "frontpage" activity after that.
Whenever I switch away from the application and resume it either by relaunching it or using the application switcher, it will always re-open the log in activity. Even if I switch to my app from within the frontpage activity, it opens the log in activity.
None of my other apps have ever done this, even though they obviously all have a filter for the launcher in AndroidManifest.xml. I am aware that it is supposed to do this when the activity is killed, but I cannot comprehend why it would do that.
This is my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="nl.vertinode.facepunch"
android:versionCode="1"
android:versionName="1.0">
<!-- Android 2.2 -->
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" />
<!-- Internet access -->
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="#drawable/icon" android:label="#string/app_name" android:name=".FPApp">
<!-- Login form -->
<activity android:name="nl.vertinode.facepunch.LoginActivity" android:launchMode="singleTask" android:theme="#android:style/Theme.Light.NoTitleBar" android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Frontpage view -->
<activity android:name="nl.vertinode.facepunch.FrontpageActivity" android:theme="#android:style/Theme.Light.NoTitleBar" android:windowSoftInputMode="stateHidden">
</activity>
</application>
</manifest>
What could I be doing wrong?
I'm going to guess this has to do with your android:launchMode being singleTask.
Try singleTop instead and see if you have the same problem. Also it could have to do with some code you have in your onPause for the other activities. Does it go back to your Login activity after going to sleep/waking back up as well?

Android securityException and user contacts

I have am wrighting an android app and as a part of the app I would like the user to be able to see, select,and modify the user contacts. My main activity extends the TabActivity for usability reasons(from the users side). So in a tab i would like to show user contacts i have done that with this code: mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("Contacts").setContent(new Intent(Intent.ACTION_PICK, People.CONTENT_URI)));
that uses the default phone contact activity. my android manifest is: `
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.READ_OWNER_DATA"/>
<application android:icon="#drawable/icon" android:theme="#android:style/Theme.NoTitleBar">
<activity android:name=".WaveCally"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".streamer"
android:label="#string/stream">
</activity>
</application>
<uses-sdk android:minSdkVersion="4" />
` but I keep getting a security exception in my log and the activity crashes. any ideas?
Also as I mentioned I would like to modify the contacts (mostly add some extra fields), to do that I have to get the contantprovider and in every contact add the extra fields? would those extra fields be available if I then pick a contact from the above mentioned activity?
You need to declare in the application Manifest that your application is going to access the Contacts. (android.permission.READ_CONTACTS)
This is what you need to do:
http://developer.android.com/guide/topics/manifest/uses-permission-element.html
Basically, add the following line in your app manifest (right after opening the manifest tag):
<uses-permission android:name="android.permission.READ_CONTACTS" />

Categories

Resources