0

Lets say, we have variable var, that store information abaut name of activity. It gets it from shared preferences. I want to make some kind o autosave. Last activity that was initiated send its name do shared preferences. The thing that i dont now, is how to put the name of activity from variable var to intent in switch method in buttonlastactivity. The first buttonnew goes to first activity that is actbegin.

Example:

public void onClick(View view) {
Intent intent;

switch (view.getId()) {
case R.id.buttonnew:
intent = new Intent(actmenu.this, actbegin.class);
startActivity(intent);
break;
case R.id.buttonlastactivity:
intent = new Intent(actmenu.this, ??????.class);
startActivity(intent);
break;

The question is, what to put instead of question marks, to take name of activity that is stored in variable var.

Any suggestions?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Fore Gare
  • 1
  • 1
  • you can try this https://stackoverflow.com/questions/5754855/how-can-i-start-a-new-android-activity-using-a-string – Vivek Mishra Oct 04 '17 at 11:27
  • If you are planning to send user to particular page/activity on back press or something then its not a good practice because it will break the whole flow of application. – Akshay Katariya Oct 04 '17 at 11:45

2 Answers2

0

You could save the fully qualified class name in your shared preferences and use that .
eg. : If you save "com.test.MyLastActivity" in your SharedPreferences, then you could do this :

try {
        Class<?> lastActivity = Class.forName("com.test.MyLastActivity");
        Intent intent = new Intent(this, lastActivity);
        startActivity(intent);
    } catch (ClassNotFoundException e) {
        Log.e("TAG", "Couldn't find class", e);
    }
Neeraj
  • 2,376
  • 2
  • 24
  • 41
  • I have put the try/catch under the case R.id.buttonlastactivity: – Fore Gare Oct 04 '17 at 13:08
  • I have put the try/catch under the: case R.id.buttonlastactivity: - it make no effect after aplication start, the button pressing does nothing (no errors to). I have put a name o variable instead of name of activity: Class.forName(var); - and it makes nothing in aplication (no errors to). In addition - the variable var is changing depending on witch activity the user is last time, after he quit. – Fore Gare Oct 04 '17 at 13:16
  • Ok . My bad; didn't start the activity - `startActivity(intent);` . modified the answer. – Neeraj Oct 05 '17 at 04:54
0

I did it like this:

public void onClick(View view) {
      Intent intent;
      switch (view.getId()) {
 case R.id.buttonnew:
      intent = new Intent(actmenu.this,actbegin.class);
      startActivity(intent);
      break;
case R.id.buttonlastactivity:
       try
       {
       Class<?> lastActivity = Class.forName(var);
       intent = new Intent(actmenu.this, lastActivity);
       startActivity(intent);
       break;
       }
       catch (ClassNotFoundException e)
       {
       Log.e("TAG", "Couldn't find class", e);
}

}
}

and it doesnt give errors, but after app start and after pressing the buttonlastactivity (when variable var has full name of last activity) the app is going back to activity that starts app, not to the activity that name is written in variable var. I want to use vvariable, because it is changing depending on what activity the user was, before he quited the app. And the variable gets it from shared preferences. Shared preferences get the name of activity in moment of starting of the activity. Or is there any other way to do some kind of autosave, that will allow user to go to the last visited activity.

Fore Gare
  • 1
  • 1