简体   繁体   中英

How do i run this activity only once?

There is an activity named permissions in my app, and I want it to run only once because it is an activity where users allow permissions for the app, and once they are allowed there is no need for this activity. Now i am able to activity after permissions are granted by user but the problem is when i am on main activity and press back button i still see permissions activity. I dont want to show permissions on back press button

The relevant Java code is below:

 package com.techjapreet.shivshankarkiringtone; import android.Manifest; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.provider.Settings; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; public class permissions extends AppCompatActivity { private static final int WRITE_STORAGE_REQUEST_CODE = 123; private static final int WRITE_SETTINGS_REQUEST_CODE = 321; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.permissions); SharedPreferences preferences = getSharedPreferences("PREFERENCE", MODE_PRIVATE); String FirstTime = preferences.getString("FirstTimeInstall", ""); if(FirstTime.equals("Yes")){ Intent intent = new Intent(permissions.this, MainActivity.class); startActivity(intent); } else { SharedPreferences.Editor editor = preferences.edit(); editor.putString("FirstTimeInstall", "Yes"); editor.apply(); } findViewById(R.id.btn_do_task).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (checkBoth()) { startActivity(new Intent(permissions.this, MainActivity.class)); } else { checkStoragePermission(); } } } } ); } private boolean checkBoth() { return (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) && Settings.System.canWrite(this); } public void checkStoragePermission() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale( this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Ringtone, Read External and Write External" + " Storage permissions are required to do the task."); builder.setTitle("Please Grant those Permissions"); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { ActivityCompat.requestPermissions( permissions.this, new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE }, WRITE_STORAGE_REQUEST_CODE ); } }); builder.setNeutralButton("Cancel", null); AlertDialog dialog = builder.create(); dialog.show(); } else { // Directly request for required permissions, without explanation ActivityCompat.requestPermissions( this, new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE }, WRITE_STORAGE_REQUEST_CODE ); } } else { // Do something, when permissions are already granted Toast.makeText(this, "Write External Storage Permission Already Granted", Toast.LENGTH_SHORT).show(); if (checkBoth()) { startActivity(new Intent(this, MainActivity.class)); } else { checkSettingsPermission(); } } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case WRITE_STORAGE_REQUEST_CODE: // When request is cancelled, the results array are empty if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) { // Permissions are granted Toast.makeText(this, "Write External Storage Permission Granted.", Toast.LENGTH_SHORT).show(); } else { // Permissions are denied Toast.makeText(this, "Write External Storage Permission Denied.", Toast.LENGTH_SHORT).show(); } break; } if (checkBoth()) { startActivity(new Intent(this, MainActivity.class)); } else { checkSettingsPermission(); } } private void checkSettingsPermission() { if (!Settings.System.canWrite(this)) { Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS); intent.setData(Uri.parse("package:" + getPackageName())); startActivityForResult(intent, WRITE_SETTINGS_REQUEST_CODE); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case WRITE_SETTINGS_REQUEST_CODE: if (Settings.System.canWrite(this)) { Toast.makeText(this, "Write Settings Permission Granted.", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "Write Settings Permission Denied.", Toast.LENGTH_SHORT).show(); } break; } if (checkBoth()) { startActivity(new Intent(this, MainActivity.class)); } } }

You can add method onPause to your class:

@Override
public void onPause() {
    super.onPause();
    finish();
}

when click on back (physical or virtual) never show again this activity.

and to next run, you need to save state in file or shared-preferences and check it when is true start another activity

if (isCheckAcceptAgrement){
   startactivity(this, anotherActivity.class);
}else{
   startactivity(this, acceptAgrement.class);
}

Reason of the issue

This is because the activity is open in the memory, you are just starting the main activity without destroying the permission activity. So, when you are pressing the back button the previous activity loads as it was just on a paused state.


Solution

To destroy the activity from the memory, just do this...

Add the line >>> finish() after everytime you are typing this line >>> startActivity(new Intent(this, MainActivity.class));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM