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;
});
}
}