eshteghel company eshteghel company - 1 month ago 6
Android Question

Application exits while requesting permission

I have this piece of code in the Splash Activity that requests Read Phone State permissions to call an async task but on first run the activity finishes (not crashes) and then the permissions dialog appears, I grant permission and re enter the app, it starts normally. So why is the Splash finishing at the first run in the first place.
Here's my code

public class SplashActivity extends Activity {

boolean noConMessage = false, granted = false;
boolean firstRun;
int caller = 0;
int channelId = 0;
Bundle bundle;
String deviceId;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
MyApplication.crashBundle = this.getIntent().getExtras();
final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
MyApplication.fontSize = Integer.parseInt(settings.getString(getResources().getString(R.string.textsize_key), "15").toString());
firstRun = settings.getBoolean(getResources().getString(R.string.firstRun_key), true);
deviceId = settings.getString(getResources().getString(R.string.deviceId_key), "-1");


/*if (ContextCompat.checkSelfPermission(SplashActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(SplashActivity.this, new String[]{
Manifest.permission.READ_PHONE_STATE}, 1);
Launching mLaunching = new Launching();
mLaunching.execute();
}else{
Launching mLaunching = new Launching();
mLaunching.execute();
}*/

int hasReadPermission = ContextCompat.checkSelfPermission(SplashActivity.this, Manifest.permission.READ_PHONE_STATE);
if (hasReadPermission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(SplashActivity.this, new String[] {Manifest.permission.READ_PHONE_STATE},
123);
return;
}

// CheckNewVersionAsyncTask mCheckNewVersionAsyncTask=new CheckNewVersionAsyncTask(this);
// mCheckNewVersionAsyncTask.execute();

Launching mLaunching = new Launching();
mLaunching.execute();

}



@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 123:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
Launching mLaunching = new Launching();
mLaunching.execute();
} else {
// Permission Denied

}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}

public void loadPage() {
Intent intent;

intent = new Intent(SplashActivity.this,
ChannelListActivity.class);

intent.putExtra(Extra.IMAGES, Constants.IMAGES);
startActivity(intent);


}

protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (noConMessage) {
Toast.makeText(SplashActivity.this, "No Internet Connection", Toast.LENGTH_LONG).show();
}
finish();
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();


}

protected class Launching extends AsyncTask<Void, Void, Integer> {

@Override
protected void onPreExecute() {

}

@Override
protected Integer doInBackground(Void... a) {
try {
if (deviceId.equals("-1")) {

ServerUtilities.addDevice(SplashActivity.this);
GCMRegistrar.unregister(SplashActivity.this);
} else {
TimeUnit.SECONDS.sleep((long) 0.25);
}
if (true) {
Actions.copyFile(SplashActivity.this, "tahoma.ttf");
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;

}

@Override
protected void onPostExecute(Integer result) {
if (firstRun) {
PushNotificationActions.registerNotification(SplashActivity.this);
} else {
loadPage();
}

}
}


}

Answer

Remove this finish(); call from onpause function because when dialog appear your activity will go in onpause state and the finish call will destroy your activity