ANDROID STUDIO How to go back to application from settings

Viewed 37

I got some trouble with android studio I work in a company who has a web application so I worked on IOS application it was pretty easy but now I worked on Android and I'm not able to do a simple return to the application.

This the visual legacy :

When you open the application it answers you to allow some permission to the app :

[Popup to authorize permission] https://i.stack.imgur.com/Uz9Ox.png

Then you go to the settings : [Settings] https://i.stack.imgur.com/oqlfG.png

As you can see we implement additional settings to enter user ids ( mail, password ) :

[Additional settings ] https://i.stack.imgur.com/zn8Ll.png

You can see that there is a button below the inputs I want it to returns to the application but when I'm putting this code

Preference myPref = findPreference("backto");
myPref.setOnPreferenceClickListener(preference -> {
   startActivity(new Intent(getContext(), MainActivity.class));
   return true;
});

It doesn't send me to the application it creates a new instance of the application directly in the additional settings.

How can I make a function that send me to the app and not create a new instance ?

There is my additional settings class:

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;

/**
 * A simple {@link Fragment} subclass.
 */
public class SettingsFragment extends PreferenceFragmentCompat {

    MainActivity mainActivity;

    @Override
    public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
        setPreferencesFromResource(R.xml.preferences, rootKey);

        Preference myPref = findPreference("backto");
        myPref.setOnPreferenceClickListener(preference -> {
            startActivity(new Intent(getContext(), MainActivity.class));
            return true;
        });
    }

}
1 Answers

If you want to request permissions from your app, the proper way is through Intent and then you handle the code in onRequestPermissionsResult. There is no need to direct the user to the settings

See these tutorials for more info. Just keep in mind that Google now has a new policy about "sensitive permissions", some of them may not be approved when uploaded on Google Play

Link 1,

Link 2,

Link 3

Related