0

I'm bit stuck on this one, it's not too complicated but it's got me beaten! What I'm trying to do is resume the user's session, by getting the name of the last activity and then passing it to an intent.
Where I'm stuck, is in converting the retrieved String into a classname, so the resumeIntent can use it.

   public void Resume (View view){
    SharedPreferences sharedPref =              
    PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
    String resumeName = sharedPref.getString("ActivityName", null);
    //probably need to do something here//
    Intent resumeIntent = new Intent (this, resumeName);
    startActivity(resumeIntent);}
2
  • 2
    have you tried new Intent(this, Class.forName(resumeName)) ?? Commented Feb 18, 2017 at 14:40
  • Thankyou very much, it works perfectly :) Commented Feb 18, 2017 at 15:33

1 Answer 1

4

Try ::

Intent resumeIntent = new Intent (this, Class.forName(getPackageName() + resumeName);
startActivity(resumeIntent);

UPDATE

String resumeName = YourActivityName.class.getCanonicalName();
try {
    Class newClass = Class.forName(resumeName);
    Intent resume = new Intent(this, newClass);
    startActivity(resume);
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}

Store the canonical name of the activity in the string variable.

Sign up to request clarification or add additional context in comments.

2 Comments

Thankyou, that was helpful.
Not to mention. If it was helpful and solved your problem, please mark this as an accepted answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.