Clearing the SharedPreference data after some specified time












0














I am using SharedPreference in my application. When application is started I am saving some data in it. I have one Activity on which I am performing all Fragment operations like adding, replacing etc. I need to remove that stored data from preference if I am not performing any actions in application i.e. application is idle for some time. How to detect that user is not performing some operations in app and remove data from preference after some specified time when app is stable?










share|improve this question
























  • Have you checked this?
    – Piyush
    Dec 27 at 13:25










  • One way to use rx. This library can be useful for your task github.com/JakeWharton/RxBinding
    – Artem Botnev
    Dec 27 at 13:28
















0














I am using SharedPreference in my application. When application is started I am saving some data in it. I have one Activity on which I am performing all Fragment operations like adding, replacing etc. I need to remove that stored data from preference if I am not performing any actions in application i.e. application is idle for some time. How to detect that user is not performing some operations in app and remove data from preference after some specified time when app is stable?










share|improve this question
























  • Have you checked this?
    – Piyush
    Dec 27 at 13:25










  • One way to use rx. This library can be useful for your task github.com/JakeWharton/RxBinding
    – Artem Botnev
    Dec 27 at 13:28














0












0








0







I am using SharedPreference in my application. When application is started I am saving some data in it. I have one Activity on which I am performing all Fragment operations like adding, replacing etc. I need to remove that stored data from preference if I am not performing any actions in application i.e. application is idle for some time. How to detect that user is not performing some operations in app and remove data from preference after some specified time when app is stable?










share|improve this question















I am using SharedPreference in my application. When application is started I am saving some data in it. I have one Activity on which I am performing all Fragment operations like adding, replacing etc. I need to remove that stored data from preference if I am not performing any actions in application i.e. application is idle for some time. How to detect that user is not performing some operations in app and remove data from preference after some specified time when app is stable?







android android-fragments android-activity sharedpreferences






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 27 at 14:02









Fantômas

32.3k156288




32.3k156288










asked Dec 27 at 13:21









Satyam

178112




178112












  • Have you checked this?
    – Piyush
    Dec 27 at 13:25










  • One way to use rx. This library can be useful for your task github.com/JakeWharton/RxBinding
    – Artem Botnev
    Dec 27 at 13:28


















  • Have you checked this?
    – Piyush
    Dec 27 at 13:25










  • One way to use rx. This library can be useful for your task github.com/JakeWharton/RxBinding
    – Artem Botnev
    Dec 27 at 13:28
















Have you checked this?
– Piyush
Dec 27 at 13:25




Have you checked this?
– Piyush
Dec 27 at 13:25












One way to use rx. This library can be useful for your task github.com/JakeWharton/RxBinding
– Artem Botnev
Dec 27 at 13:28




One way to use rx. This library can be useful for your task github.com/JakeWharton/RxBinding
– Artem Botnev
Dec 27 at 13:28












1 Answer
1






active

oldest

votes


















-1














You can achieve it by adding thread on your parent activity / anywheve in the app which you or your app can access without any object initialization.



Declare Handler



private Handler handlerClearPref;


Add inside onCreate()



handlerClearPref = new Handler();
handlerClearPref.postDelayed(runnableClearPref, 20000);


Add inside Activity class out out of onCreate Method



private Runnable runnableClearPref = new Runnable() {

@Override
public void run() {
// Code to clear your Shared Preference contents.
handlerCheckEventNotification.postDelayed(runnableClearPref, 20000);
}
};


Above code will clear your prefrence at every 20 second.



And if you want to stop this thread then you can use below code.



handlerClearPref.removeCallbacks(runnableClearPref);





share|improve this answer










New contributor




Ajay Mehta - Rlogical is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • How does this answer handles the inactivity functionality which is requested?
    – Nero
    Dec 27 at 14:28










  • Yes, you are correct. I have answered this for the query related to clear preference at specified time. For the user's inactivity, i think @satyam should go with the link provided by Piyus in comment.
    – Ajay Mehta - Rlogical
    Dec 27 at 14:39











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53945790%2fclearing-the-sharedpreference-data-after-some-specified-time%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














You can achieve it by adding thread on your parent activity / anywheve in the app which you or your app can access without any object initialization.



Declare Handler



private Handler handlerClearPref;


Add inside onCreate()



handlerClearPref = new Handler();
handlerClearPref.postDelayed(runnableClearPref, 20000);


Add inside Activity class out out of onCreate Method



private Runnable runnableClearPref = new Runnable() {

@Override
public void run() {
// Code to clear your Shared Preference contents.
handlerCheckEventNotification.postDelayed(runnableClearPref, 20000);
}
};


Above code will clear your prefrence at every 20 second.



And if you want to stop this thread then you can use below code.



handlerClearPref.removeCallbacks(runnableClearPref);





share|improve this answer










New contributor




Ajay Mehta - Rlogical is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • How does this answer handles the inactivity functionality which is requested?
    – Nero
    Dec 27 at 14:28










  • Yes, you are correct. I have answered this for the query related to clear preference at specified time. For the user's inactivity, i think @satyam should go with the link provided by Piyus in comment.
    – Ajay Mehta - Rlogical
    Dec 27 at 14:39
















-1














You can achieve it by adding thread on your parent activity / anywheve in the app which you or your app can access without any object initialization.



Declare Handler



private Handler handlerClearPref;


Add inside onCreate()



handlerClearPref = new Handler();
handlerClearPref.postDelayed(runnableClearPref, 20000);


Add inside Activity class out out of onCreate Method



private Runnable runnableClearPref = new Runnable() {

@Override
public void run() {
// Code to clear your Shared Preference contents.
handlerCheckEventNotification.postDelayed(runnableClearPref, 20000);
}
};


Above code will clear your prefrence at every 20 second.



And if you want to stop this thread then you can use below code.



handlerClearPref.removeCallbacks(runnableClearPref);





share|improve this answer










New contributor




Ajay Mehta - Rlogical is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • How does this answer handles the inactivity functionality which is requested?
    – Nero
    Dec 27 at 14:28










  • Yes, you are correct. I have answered this for the query related to clear preference at specified time. For the user's inactivity, i think @satyam should go with the link provided by Piyus in comment.
    – Ajay Mehta - Rlogical
    Dec 27 at 14:39














-1












-1








-1






You can achieve it by adding thread on your parent activity / anywheve in the app which you or your app can access without any object initialization.



Declare Handler



private Handler handlerClearPref;


Add inside onCreate()



handlerClearPref = new Handler();
handlerClearPref.postDelayed(runnableClearPref, 20000);


Add inside Activity class out out of onCreate Method



private Runnable runnableClearPref = new Runnable() {

@Override
public void run() {
// Code to clear your Shared Preference contents.
handlerCheckEventNotification.postDelayed(runnableClearPref, 20000);
}
};


Above code will clear your prefrence at every 20 second.



And if you want to stop this thread then you can use below code.



handlerClearPref.removeCallbacks(runnableClearPref);





share|improve this answer










New contributor




Ajay Mehta - Rlogical is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









You can achieve it by adding thread on your parent activity / anywheve in the app which you or your app can access without any object initialization.



Declare Handler



private Handler handlerClearPref;


Add inside onCreate()



handlerClearPref = new Handler();
handlerClearPref.postDelayed(runnableClearPref, 20000);


Add inside Activity class out out of onCreate Method



private Runnable runnableClearPref = new Runnable() {

@Override
public void run() {
// Code to clear your Shared Preference contents.
handlerCheckEventNotification.postDelayed(runnableClearPref, 20000);
}
};


Above code will clear your prefrence at every 20 second.



And if you want to stop this thread then you can use below code.



handlerClearPref.removeCallbacks(runnableClearPref);






share|improve this answer










New contributor




Ajay Mehta - Rlogical is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this answer



share|improve this answer








edited 2 days ago









Android

32113




32113






New contributor




Ajay Mehta - Rlogical is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









answered Dec 27 at 13:43









Ajay Mehta - Rlogical

514




514




New contributor




Ajay Mehta - Rlogical is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Ajay Mehta - Rlogical is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Ajay Mehta - Rlogical is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • How does this answer handles the inactivity functionality which is requested?
    – Nero
    Dec 27 at 14:28










  • Yes, you are correct. I have answered this for the query related to clear preference at specified time. For the user's inactivity, i think @satyam should go with the link provided by Piyus in comment.
    – Ajay Mehta - Rlogical
    Dec 27 at 14:39


















  • How does this answer handles the inactivity functionality which is requested?
    – Nero
    Dec 27 at 14:28










  • Yes, you are correct. I have answered this for the query related to clear preference at specified time. For the user's inactivity, i think @satyam should go with the link provided by Piyus in comment.
    – Ajay Mehta - Rlogical
    Dec 27 at 14:39
















How does this answer handles the inactivity functionality which is requested?
– Nero
Dec 27 at 14:28




How does this answer handles the inactivity functionality which is requested?
– Nero
Dec 27 at 14:28












Yes, you are correct. I have answered this for the query related to clear preference at specified time. For the user's inactivity, i think @satyam should go with the link provided by Piyus in comment.
– Ajay Mehta - Rlogical
Dec 27 at 14:39




Yes, you are correct. I have answered this for the query related to clear preference at specified time. For the user's inactivity, i think @satyam should go with the link provided by Piyus in comment.
– Ajay Mehta - Rlogical
Dec 27 at 14:39


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53945790%2fclearing-the-sharedpreference-data-after-some-specified-time%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas