I’ve got an android application that I want to open a different activity then my main one after a fresh install.
I tried this using startActivityForResult() and SharedPreferences. Here is my code:
main activity:
public class ONTTMainActivity extends Activity {
static final int REQUEST_CODE = 5;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences settings = getSharedPreferences("prefs", 0);
boolean firstRun = settings.getBoolean("firstRun", true);
if(firstRun){
startActivityForResult(
new Intent(this, ONTTSplashActivity.class), REQUEST_CODE);
}
setContentView(R.layout.activitymain);
}
second activity:
public class ONTTSplashActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activityonttsplash);
final Button btnSkip = (Button) findViewById(R.id.button_skip);
btnSkip.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setResult(RESULT_OK);
finish();
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(ONTTSplashActivity.this, "Toast Reached", Toast.LENGTH_LONG).show();
if (resultCode == RESULT_OK) {
SharedPreferences settings = getSharedPreferences("ONTT_prefs", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstRun", false);
editor.commit();
}
}
The problem is that even though the second activity is ended the code in the onActivityResult function is never executed. I tried using a toast to see if it has been reached.
I’ve seen several similar questions but I’ve tried every solution but it’s not working.
You are starting
ONTTSplashActivityfromONTTMainActivityfor getting result back from ONTTSplashActivity to ONTTMainActivity so putonActivityResultin firstONTTMainActivityactivity: