GOAL: when the user press back on SettingsActivity toolbar the previous Activity should be resumed from where the user left.
PROBLEM: From Activity lifecycle reported on android developer website I understand that the previous Activity should be resumed calling OnResume method, instead in my case the MainActivity start again calling OnCreate method.
In particular the flow is as follows:
1) User click on icon to start SettingsActivity
2)MapsActivity invokes onPause, then onSaveInstanceState, then onStop
3) User click back button on SettingsActivity
4)MapsActivity invokes onDestroy then onCreate (and everything I tried to save during point 2 with saveInstanceState is lost because Bundle is always null)
CODE
MapsActivity (main activity)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// .... other initialization code ... //
// If location permission is granted initialize Map
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mapSync();
} else {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_FINE_LOCATION);
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("prova", true);
}
// Where I start the second activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.connection_type:
// .... handle this case ... //
case R.id.settings:
Intent intent = new Intent(MapsActivity.this, SettingsActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
SettingsActivity (called activity)
public class SettingsActivity extends AppCompatActivity
{
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
// Set the toolbar
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
myToolbar.setTitle("Settings");
myToolbar.setNavigationIcon(R.drawable.ic_baseline_arrow_back_24px);
myToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
setSupportActionBar(myToolbar);
}
public static class MainSettingsFragment extends PreferenceFragmentCompat {
public final static String KEY_ENABLE_BACKGROUND_UPDATE = "enable_background_update";
public final static String KEY_ENABLE_LAST_KNOWN_LOCATION = "enable_last_known_location";
public final static String KEY_DELETE_DB_DATA = "delete_db_data";
public final static String KEY_CHANGE_MAP_COLOR ="change_map_color";
private SharedPreferences.OnSharedPreferenceChangeListener preferenceChangeListener;
#Override
public void onCreatePreferences(Bundle bundle, String s) {
// Load the Preferences from the XML file
addPreferencesFromResource(R.xml.preferences);
preferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if(key.equals(KEY_DELETE_DB_DATA))
{
String connectivityType = sharedPreferences.getString(key, null);
new DeleteAreaAsync(SignalAreaDatabase.getDatabase(getContext()), connectivityType).execute();
} else if(key.equals(KEY_CHANGE_MAP_COLOR)){
String gradientColor = sharedPreferences.getString(key, null);
SignalAreaDrawer signalAreaDrawer = SignalAreaDrawer.getSignalAreaDrawer();
signalAreaDrawer.setGradientColor( gradientColor);
}
}
};
}
#Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(preferenceChangeListener);
}
#Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(preferenceChangeListener);
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Less accurate location: telephony manager and location requests -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- Access to wifi network information -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- Change wifi connectivity state -->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:parentActivityName=".MapsActivity"
android:theme="#style/PreferenceScreen" />
</application>
QUESTION: how can I restore the previous Activity state and show to the user exactly what he was visualizing when he opened the second Activity?
EDIT #1:
I tried to override oonDestroy() , onSaveInstance(), onRestoreInstance() and this is what happens:
when I start the setting activity, the main activity go onPause() as I would expect.
when I press the back button on the settings activity , the main activity go before onDestroy() and immediately after onCreate(), not calling onSaveInstance() or onRestoreInstance() at all.
EDIT #2: the app didn't go through onSaveInstanceState(Bundle outState) probably because I declared it public. Now the app calls it. So I tried to save some info like outState.putBoolean("prova", true); but when the mainActivity is destroyed , at new onCreate(Bundle savedInstanceState) invokation the Bundle savedInstanceState is always null.
EDIT #3: as #SherifelKhatib suggested I tried to delete all finish() statement from MapsActivity, and I tried to substitute MapsActivity with a minimal EmptyActivity to see if the problem was in MapsActivity. Unfortunately the app has the same behaviour.
When the user press the back button the previous app is always destroyed. No way to restore its state.
EDIT #4: what I tried and still doesn't work. Modifying SettingsActivity :
First approach
#Override
public void onBackPressed(){
moveTaskToBack(true);
}
Second approach
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
Third approach
#Override
public void onBackPressed() {
Intent backIntent = new Intent(this, MapsActivity.class);
backIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(backIntent);
}
And add this in MainActivity:
Intent intent = new Intent(MapsActivity.this, SettingsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
SOLUTION:
Add this attribute to MainActivity in the manifest .
android:launchMode="singleTop"
Explanation
The "standard" and "singleTop" modes differ from each other in just
one respect: Every time there's a new intent for a "standard"
activity, a new instance of the class is created to respond to that
intent. Each instance handles a single intent. Similarly, a new
instance of a "singleTop" activity may also be created to handle a new
intent. However, if the target task already has an existing instance
of the activity at the top of its stack, that instance will receive
the new intent (in an onNewIntent() call); a new instance is not
created.
Related
I use NotificationManager, NotificationChannel and NotificationCompat.Builder to display a push notification when an event is triggered, no online Firebase.
The push notification appears when my app is in foreground and background, but it only should appear when my app in the background.
I have 2 Activities where just only 1 knows the class, where the push notification is created. I could just set a variable that shows if my Activity is in foreground or not (with onResume and onPause), but if my other Activity starts the Activity with the notification trigger class is set in background and therefore I can't use this method. I would need to know if the whole app is in foreground or not, but I didn't find a good solution for that problem.
So is there an other way to display push notifications just when my whole app is in background?
I think you have FCMListenerService to receive your push notification as a background service. The Service should have the following declaration in the manifest file.
<service android:name=".Service.FCM.FCMListenerService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
Now you can pass the context of this service alone to check if the application is in foreground or in background. The function for checking if the application is foreground is following.
private static boolean isForeground(Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTaskInfo = manager.getRunningTasks(1);
ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
return componentInfo.getPackageName().equals(Constants.ApplicationPackage);
}
Hence, in your onMessageReceived function you need to check if the application is in foreground and do not create the notification in the status bar.
public class FCMListenerService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage message) {
if(!isForeground(this)) createNotification();
}
}
Hope that helps!
So I found a proper and very good solution for this problem.
According to this answer: https://stackoverflow.com/a/42679191/6938043
Application.ActivityLifecycleCallbacks
public class MainActivityChats extends AppCompatActivity implements Application.ActivityLifecycleCallbacks
{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getApplication().registerActivityLifecycleCallbacks(this);
}
public void onActivityResumed(Activity activity)
{
if(msg_updater != null)
{
msg_updater.set_app_is_active(true);
}
}
public void onActivityPaused(Activity activity)
{
if(msg_updater != null)
{
msg_updater.set_app_is_active(false);
}
}
public void onActivityStopped(Activity activity) {}
public void onActivityCreated(Activity activity, Bundle bundle) {}
public void onActivityStarted(Activity activity) {}
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {}
public void onActivityDestroyed(Activity activity) {}
}
i have an adapter in activity and its arrayList
private ArrayList<String> data = new ArrayList<>();
and i have brodcastReciver too when i get data i add it to the ArrayList data
data.add("bla bla bla"); // brodcast data
and update adapter
but my adapter not updated when activity its paused and i missed the brodcast data
so what the solution for this i just made some search and found this answer but he did't share the idea
my app idea its chatting app with sqlite of Course and the problem i missed the BrodcastEvent when activity its paused and get the data from sqlite every time activity its resumed i see it as bad idea
The BroadcastReceiver would receive event without activity start or resume If register it in your manifest.
<receiver android:name="YourReceiver" >
<intent-filter>
<action android:name="android.intent.action.YourAction" />
</intent-filter>
</receiver>
Another issue, how BroadcastReceiver notify message adapter.
You could define a singleton object as observer register manager. If activity start, it register itself, and BroadcastReceiver notify it when receive new message.
BroadcastReceiver
#Override
public void onReceive(Context context, Intent intent) {
...
SingletonRegister.getInstance().addMessage("bla bla bla");
}
Activity
protected void onCreate(Bundle savedInstanceState) {
...
SingletonRegister.getInstance().observe(this);
}
#Override
public void update(String message) {
data.add(message);
}
If activity don't start, activity would not get notified because of not registered. In order to ensure receive all messages, activity need actively request last messages after register.
Activity
protected void onCreate(Bundle savedInstanceState) {
...
SingletonRegister.getInstance().observe(this);
requestMessageFromServer();
}
I'd like to implement an update checker in an application, and I obviously only need this to show up once when you start the application. If I do the call in the onCreate() or onStart() method, it'll be shown every time the activity is created and this is not a viable solution.
So my question is: Is there a way to do something, like check for updates, just once per application start / launch?
I'm sorry if it's a bit hard to understand, I'm having difficulties explaning myself on this one.
SharedPreferences seems like ugly solution to me. It's much more neat when you use application constructor for such purposes.
All you need is to use your own Application class, not default one.
public class MyApp extends Application {
public MyApp() {
// this method fires only once per application start.
// getApplicationContext returns null here
Log.i("main", "Constructor fired");
}
#Override
public void onCreate() {
super.onCreate();
// this method fires once as well as constructor
// but also application has context here
Log.i("main", "onCreate fired");
}
}
Then you should register this class as your application class inside AndroidManifest.xml
<application android:label="#string/app_name" android:name=".MyApp"> <------- here
<activity android:name="MyActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
You even can press Back button, so application go to background, and will not waste your processor resources, only memory resource, and then you can launch it again and constructor still not fire since application was not finished yet.
You can clear memory in Task Manager, so all applications will be closed and then relaunch your application to make sure that your initialization code fire again.
looks like you might have to do something like this
PackageInfo info = getPackageManager().getPackageInfo(PACKAGE_NAME, 0);
int currentVersion = info.versionCode;
this.versionName = info.versionName;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int lastVersion = prefs.getInt("version_code", 0);
if (currentVersion > lastVersion) {
prefs.edit().putInt("version_code", currentVersion).commit();
// do the activity that u would like to do once here.
}
You can do this every time, to check if the app has been upgraded, so it runs only once for app upgrade
The shared preferences approach is messy, and the application class has no access to an activity.
Another alternative I've used is to have a retained fragment instance, and within that instance, a lot more stuff can be done especially if you need access to the main activity UI.
For this example, I've used asynctask within the retained fragment. My AsyncTask has callbacks to the parent activity. It is guaranteed to run only once per application because the fragment is never destroyed-recreated when the same activity is destroyed-recreated. It is a retained fragment.
public class StartupTaskFragment extends Fragment {
public interface Callbacks {
void onPreExecute();
void onProgressUpdate(int percent);
void onCancelled();
void onPostExecute();
}
public static final String TAG = "startup_task_fragment";
private Callbacks mCallbacks;
private StartupTask mTask;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mCallbacks = (Callbacks) activity;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true); // this keeps fragment in memory even if parent activity is destroyed
mTask = new StartupTask();
mTask.execute();
}
#Override
public void onDetach() {
super.onDetach();
mCallbacks = null;
}
private class StartupTask extends AsyncTask<Void, Integer, Void> {
#Override
protected void onPreExecute() {
if (mCallbacks != null) {
mCallbacks.onPreExecute();
}
}
#Override
protected Void doInBackground(Void... ignore) {
// do stuff here
return null;
}
#Override
protected void onProgressUpdate(Integer... percent) {
if (mCallbacks != null) {
mCallbacks.onProgressUpdate(percent[0]);
}
}
#Override
protected void onCancelled() {
if (mCallbacks != null) {
mCallbacks.onCancelled();
}
}
#Override
protected void onPostExecute(Void ignore) {
if (mCallbacks != null) {
mCallbacks.onPostExecute();
}
}
}
}
Then, in main (or parent) activity where you want this startup task fragment to run once.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fm = getFragmentManager();
StartupTaskFragment st = (StartupTaskFragment) fm.findFragmentByTag(StartupTaskFragment.TAG);
if(st == null) {
fm.beginTransaction().add(mStartupTaskFragment = new StartupTaskFragment(), StartupTaskFragment.TAG).commit();
}
...
}
Ideas for retained fragment came from here: http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html. I just figured out its other uses aside from config changes.
Yes you can do it Using SharedPrefernce concept of android. Just create a boolean flag and save it in SharedPrefernce and check its value in your onCreate() method .
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (!prefs.getBoolean("onlyonce", false)) {
// <---- run your one time code here
// mark once runned.
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("onlyonce", true);
editor.commit();
}
}
This continues on #Vitalii's answer.
After having setup the Application class, if access to the Activity is required, we can use the aptly named android library "Once" https://github.com/jonfinerty/Once.
In the Application class's onCreate method
Once.initialise(this)
In the Activity / Fragment class's onCreate / onViewCreated method.
val helloTag = "hello"
if (!Once.beenDone(Once.THIS_APP_SESSION, helloTag)) {
//Do something that needs to be done only once
Once.markDone(helloTag) //Mark it done
}
I do this the same way as described in the other answer. I just have a global variable in the first activity which matches the release number from the manifest. I increment it for every upgrade and when the check sees a higher number, it executes the one-time code.
If successful, it writes the new number to shared preferences so it wont do it again until the next upgrade.
Make sure you assign the default to -1 when you retrieve the version from shared preferences so that you error on the side of running the code again as opposed to not running it and not having your app update correctly.
Use SharedPreference for this-
If you are not restarting your launcher activity again once your app is active then in that case you case use it.
Use this in a Splash screen if you are implementing it in the app.
If you are not using any splash screen then you need to create a activity with no view set and on it's oncreate call you can do start updation and start your main activity.
you can use counter value or boolean for this.
Here is SharedPreference doc:
http://developer.android.com/reference/android/content/SharedPreferences.html
try {
boolean firstboot = getSharedPreferences("BOOT_PREF",MODE_PRIVATE)
.getBoolean("firstboot", true);
if(firstboot){
//place your code that will run single time
getSharedPreferences("BOOT_PREF",MODE_PRIVATE).edit().
putBoolean("firstboot", false)
.commit();
}
I just solved doing this myself, I reopen my main activity multiple times throughout the application's execution. While the constructor is a valid approach for some things it doesn't let you access the current Application context to write toasts among other things.
My solution was to create a simple 'firstRun' boolean set to true in the class of my MainActivity, from there I run the contents of the if statement then set it to true. Example:
public class MainActivity extends AppCompatActivity
{
private static boolean firstRun = true;
#Override
protected void onCreate(Bundle savedInstanceState)
{
if(firstRun)
{
Toast.makeText(getApplicationContext(), "FIRST RUN", Toast.LENGTH_SHORT).show();
//YOUR FIRST RUN CODE HERE
}
firstRun = false;
super.onCreate(savedInstanceState);
//THE REST OF YOUR CODE
}
I have an Android App, which shows a "Splash Screen" for 3 seconds. After that, the MainActivity gets loaded.
Unfortunately the MainActivity takes additional ~4 seconds to load. On first startup even longer. However when the App is loaded, everything runs smooth.
Now how can I achieve it, that the MainActivity gets loaded, during the display of the Splash Screen? It just should display an image until the whole thing loaded completely.
I have read about Async-Task, but I'm not sure where to put it and how to use it properly. Can someone help me please?
SplashScreen.java
public class SplashScreen extends Activity {
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_startup);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}
}
MainActivity.java
public class MainActivity extends Activity implements OnClickListener, MediaController.MediaPlayerControl {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Some heavy processing
//starting services
//starting Google Text to Speech
//and so on...
}
}
You should not be creating a new thread on startup, instead you should create a view that does not have to wait for your resources to load, as detailed in this article: Splash Screens the Right Way.
As stated in the article, you should create a layer-list drawable instead of a layout XML file:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Fill the background with a solid color -->
<item android:drawable="#color/gray"/>
<!-- Place your bitmap in the center -->
<item>
<bitmap
android:gravity="center"
android:src="#mipmap/ic_launcher"/>
</item>
</layer-list>
Then create a theme using the drawable file as a background. I use the background attribute instead of the windowBackground attribute as suggested in the article, because background takes the status and navigation bars into account, centering the drawable better. I also set windowAnimationStyle to null so that the splash screen does not animate the transition to the MainActivity:
<resources>
<!-- Base application theme -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<!-- Splash Screen theme -->
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:background">#drawable/background_splash</item>
<item name="android:windowAnimationStyle">#null</item>
</style>
</resources>
Then declare your theme in the manifest for your SplashActivity:
<activity android:name=".SplashActivity"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And finally all you have to do in your SplashActivity is start your MainActivity, and the splash screen will only show for as long as it takes for your app to configure:
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
If there are no specific constraints about the time the splash screen should be shown, you could use the AsyncTask in the following way:
public class SplashScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_startup);
startHeavyProcessing();
}
private void startHeavyProcessing(){
new LongOperation().execute("");
}
private class LongOperation extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
//some heavy processing resulting in a Data String
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.interrupted();
}
}
return "whatever result you have";
}
#Override
protected void onPostExecute(String result) {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
i.putExtra("data", result);
startActivity(i);
finish();
}
#Override
protected void onPreExecute() {}
#Override
protected void onProgressUpdate(Void... values) {}
}
}
If the resulting data if of another nature than a String you could put a Parcelable Object as an extra to your activity. In onCreate you can retrieve the data with:
getIntent().getExtras.getString('data');
How about, in the interest of simplicity, you combine your splash activity with your main activity? That way you get the best of both worlds, namely a splash screen while your data is preparing the first time, and a quick startup when it's been prepared previously. Making the user wait for nothing is not really good form...
Something like:
public class MainActivity extends Activity implements OnClickListener, MediaController.MediaPlayerControl {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initially shows splash screen, the main UI is not visible
setContentView(R.layout.activity_main);
// Start an async task to prepare the data. When it finishes, in
// onPostExecute() get it to call back dataReady()
new PrepareDataAsyncTask(this).execute();
}
public void dataReady() {
// Hide splash screen
// Show real UI
}
}
your splash screen code works fine but when you call next activity then in onCreate() use Asynctask for your heavy tasks...
I have had similar problem. There was a blank loading screen (not even toolbar). Mu culprit was in the manifest in the MainActivity:
android:launchMode="singleInstance"
Just do like in this article:
1 - create a XML layout like this for the splash screen. I called it "background_splash.xml"
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/cardview_light_background"/>
<item>
<bitmap
android:gravity="center"
android:src="#drawable/kiss_com_sub_logo"/>
</item>
</layer-list>
2 - Then, go to the styles.xml and write a style like this:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/background_splash</item>
</style>
3 - Write an activity to your splash. I called it SplashActivity.kt
class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
}
}
4 - Finally, go to you AndroidManifest.xml and add your activity splash: (Note: don't remove anything in the AndroidManifest, just add this before the Main activity).
<activity
android:name=".SplashActivity"
android:label="Kiss"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
This is done. You don't need to worry about the time that your application will demand to start, the splash will be there just for enough time. When your MainActivity is ready, it will be showed.
Ok so every post i find i cannot get this to work, i am trying to inside my list preference
Settingsbasic.xml
<ListPreference
android:title="themes"
android:summary="Select the option of the list"
android:key="listPref"
android:entries="#array/Themes"
android:entryValues="#array/list"
android:defaultValue="default" />
Now about as you can see above this is my listpreference inside my settingsbasic.xml file. Now what i need to know how to do is i have 2 java files, my main activity. and my preferences java file. I need to know how i can when the user clicks one of the the entries it does something, likes opens something or changes the ui, just something i think i can take it from there. I just need to know how and where the code would go. inside the main acitivty or preference activity.
here is my preference java file
public class Prefs extends PreferenceActivity {
ListPreference listPref;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settingsbasic);
}
protected void onResume() {
super.onResume();
// Registers a callback to be invoked whenever a user changes a preference.
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
protected void onPause() {
super.onPause();
// Unregisters the listener set in onResume().
// It's best practice to unregister listeners when your app isn't using them to cut down on
// unnecessary system overhead. You do this in onPause().
getPreferenceScreen()
.getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// Sets refreshDisplay to true so that when the user returns to the main
// activity, the display refreshes to reflect the new settings.
WebViewClientDemoActivity.????? = true;
}
}
Any example code would help, or adding on to my code above. I just need someone whom can shed some light on this code. i've tried so many different things and none of it works.
Ok so using the method of the sample app that was recommended below here is some more code i have .
Main Activity
public class WebViewClientDemoActivity extends Activity {
public static String sPref = null;
public void onStart() {
super.onStart();
// Gets the user's network preference settings
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
// Retrieves a string value for the preferences. The second parameter
// is the default value to use if a preference value is not found.
sPref = sharedPrefs.getString("listPref", "Default");
}
This is the way I used PreferenceActivity:
public class EditPrefs extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.screen_prefs);
}
}
Then in /res/xml folder I have the XML file:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:key="txtColor"
android:title="textView Color"
android:summary="select color for textViews"
android:entries="#array/txtColor"
android:entryValues="#array/txtColorValues" />
</PreferenceScreen>
And in /res/values I have this XML file includes items and their values:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="txtColor">
<item>blue</item>
<item>brown</item>
<item>gray</item>
<item>violet</item>
</string-array>
<string-array name="txtColorValues">
<item>#ff000099</item>
<item>#5F1E02</item>
<item>#333333</item>
<item>#421C52</item>
</string-array>
</resources>
Then I easily call this class from another android class for instance when user clicks on a menu item:
startActivity(new Intent(this, EditPrefs.class));
You can call the preferences in onCreate and onResume like:
#Override
protected void onResume() {
super.onResume();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String txtColor = prefs.getString("txtColor", DEFAULT COLOR); // for instanse : #ff000099
textView.setBackgroundColor(Color.parseColor(txtColor));
}
I have resolved my question by this.
Prefs
public static String theme = "Theme1";
#Override
public void onBackPressed() {
ListPreference listPreference = (ListPreference) findPreference("listPref");
String currValue = listPreference.getValue();
theme = currValue;
super.onBackPressed();
}
Main Activity
if (Prefs.theme.equals("Theme1"))
setContentView(R.layout.main);
else
setContentView(R.layout.main2);
Preference XML
<ListPreference
android:title="themes"
android:summary="Select the option of the list"
android:key="listPref"
android:entries="#array/listReturnValue"
android:entryValues="#array/listDisplayWord" />