title: Android kotlin / java - logout: clear shared preferences and stop all processes in background
body: <p>After I clear all shared preferences and send the user to main activity:</p>
  
  <pre><code>            this.getSharedPreferences("userData", 0).edit().clear().apply()
  
  
              finishAffinity()
              startActivity(Intent(this, MainActivity::class.java))
  </code></pre>
  
  <p>The app crashes becauzse there are still couple of tasks/timers in the background associated with the preferences. How can you just finish EVERYTHING before I send the user to main activity?</p>
  
  <p>I tried this:</p>
  
  <pre><code>            val intent = Intent(Intent.ACTION_MAIN)
              intent.addCategory(Intent.CATEGORY_HOME)
              startActivity(intent)
  </code></pre>
  
  <p>but it just put the app into background and after I getting it back to foreground it crashes. Is there any solution to just stop everything and start specific activity without finishing all background tasks one by one?</p>