Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request permission dialog blackout the activity and pressing any key is closing the activity

I dont know why this happening just for one app because i tried it on other apps it works fine. But only this app whenever i request a permission the activity blacks out and only the permission dialog appears then pressing "Allow" or "Deny" will close the activity and i need to reopen it. I am suspecting that this behavior is happening upon some varieties upon themes or extending types of the Activity like "AppCompactActivity" or "ActionBarActivity".

This is my code

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED&& ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults)
{
    switch (requestCode)
    {
        case 1:
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {
                Log.e("Permission","Granted");
            }
            else
            {
                Log.e("Permission","Not Granted");
            }

            break;
    }
}

and this is the themes but its only a suspect that the theme is the reason

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowBackground">@color/primary_orange_lighter</item>
</style>

<style name="AppTheme.Splash_Activity" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:statusBarColor">@color/primary_orange_lighter</item>
</style>

<style name="AppTheme.Login_Activity" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:statusBarColor">@color/primary_orange_lighter</item>
</style>

<style name="AppTheme.Main_Activity" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:statusBarColor">@color/primary_orange_lighter</item>
</style>


<style name="StarRatingBar" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/star_rating_bar_full</item>
    <item name="android:minHeight">48dip</item>
    <item name="android:maxHeight">48dip</item>
</style>

<style name="Dialog" parent="@android:style/Theme.Holo.Dialog.NoActionBar">
    <item name="android:color">#FFFFFFFF</item>
    <item name="android:windowBackground">@color/trans_white</item>
    <item name="android:windowMinWidthMajor">90%</item>
    <item name="android:windowMinWidthMinor">90%</item>

</style>

<style name="ToolBarStyle" parent="AppTheme.Main_Activity">
    <item name="android:statusBarColor">@color/primary_orange_lighter</item>
    <item name="android:background">@color/primary_orange_lighter</item>
    <item name="android:textColorPrimary">@android:color/white</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>

<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">false</item>
    <item name="color">@android:color/white</item>
</style>

<style name="CustomLoadingDialog" parent="SpotsDialogDefault">
    <item name="DialogTitleAppearance">@android:style/TextAppearance.Medium</item>
    <item name="DialogTitleText">Loading</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:background">@color/black</item>
    <item name="DialogSpotColor">@color/white</item>
    <item name="DialogSpotCount">5</item>
</style>


<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="SwitchButtonStyle">
    <item name="android:paddingLeft">10dp</item>
    <item name="android:paddingRight">10dp</item>
    <item name="android:paddingTop">4dp</item>
    <item name="android:paddingBottom">4dp</item>
</style>



<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

and this is the class where the request is occurring

import android.Manifest;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import syncworx.com.kunhadi_driver.R;
import syncworx.com.kunhadi_driver.classes.Constants;

public class Test_Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED&& ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
    {
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults)
{
    switch (requestCode)
    {
        case 1:
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {
                Log.e("Permission","Granted");
            }
            else
            {
                Log.e("Permission","Not Granted");
            }

            break;
    }
}
}
like image 665
Mohammad Haidar Avatar asked Aug 24 '16 09:08

Mohammad Haidar


People also ask

How do I ask permission on activity?

Permissions are granted for the application (package), not the individual activities. Simply use the sample codes given in the docs in your main activity and you will be set. The previous suggestion is alot intuitive > Your MainActivity extends to a BaseActivity while the baseactivity implements the Permission logic.

How do I request permission in Android 11?

Starting in Android 11, whenever your app requests a permission related to location, microphone, or camera, the user-facing permissions dialog contains an option called Only this time. If the user selects this option in the dialog, your app is granted a temporary one-time permission.


1 Answers

Finally after two days struggling with this issue i found out what is the problem Well let me post first the problem origin

Intent intent = new Intent(Splash_Activity.this, Test_Activity.class);
intent .addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

As you see here when i am starting the Test_Activity which is the Activity i am requesting permission in it, i am adding some flags to the intent that is starting it so that when this activity is no longer visible it will be destroyed automatically, and it seems that requesting a permission does blocks the activity as if it is no longer visible so it is getting destroyed and then there is blackout behind the permission dialog because there is no activity alive anymore and then pressing deny or allow will simply dismiss the dialog and send a callback to the onRequestPermissionsResult method but the activity is not alive to receive the callback

like image 133
Mohammad Haidar Avatar answered Sep 28 '22 03:09

Mohammad Haidar