R. Viswanathan
R. Viswanathan

Reputation: 1

Android / Sending activity context through intent

I have 5 or so activities in Android (2 of them have been shown below), which share a common Navigation Drawer. If I log in into some account from the Navigation Drawer, after successful log in, the activity which was previously showing needs to be loaded. Is it possible to send activity context through intent?

FirstActivity.java

Intent intent1 = new Intent(FirstActivity.this, Login.class);
intent1.putExtra("activity", "FirstActivity");
startActivity(intent1);
finish();

SecondActivity.java

Intent intent2 = new Intent(SecondActivity.this, Login.class);
intent2.putExtra("activity", "SecondActivity");
startActivity(intent2);
finish();

When finding the name of the activity to return in log in activity, after successful log in.

Login.java

Intent intent3 = getIntent();
String activity = intent3.getStringExtra("activity");
...
Intent intent4 = new Intent(Login.this, Class.forName(activity));
startActivity(intent4);
finish();

returns the following error message:

W/System.err: java.lang.ClassNotFoundException: Home
        at java.lang.Class.classForName(Native Method)
        at java.lang.Class.forName(Class.java:453)

Does anyone know how to fix it up?

Making use of intent1.putExtra("activity", String.valueOf(FirstActivity.this)); also does not work out either, it says that com.example.nativeapp.FirstActivity@6a7640 is an invalid class name.

Should I convert the activity context to Serializable or Parcelable or even CharSequence when I try to send those variable values through intent? Activity or AppCompatActivity does not seem to inherit Serializable or Parcelable for that to work out it seems. CharSequence does not seem to make much difference from making use of String.

I know that I can create my own class to store global variables and activity contexts and my activity can inherit from that but since my activity already inherits NavigationDrawer, my activity cannot inherit a second class. Can I declare that as an interface and inherit an interface to access global variable values from interface? Getter and setter methods, for sure cannot work out in an interface, since no implementation of functions and no declaration of variable values are allowed in an interface.

One of the reasons why I have been considering to decide to make use of a central superclass for storing variable values and changing them from subclasses whenever that I am trying to move from one activity to another is that activity contexts like this, I am not sure how to pass them through intents. That intent, which should also be able to pass on within the other central global variables of the mobile application from one activity class to another. The central superclass, such as the NavigationDrawer which is an excellent candidate since all of my Android activity classes inherit from it would be best to use if all central global variables are stored in it and they are changed from subclasses whenever that I am trying to move from one activity to another.

How do I go about it?

Upvotes: 0

Views: 1090

Answers (2)

Makarand
Makarand

Reputation: 1123

you are trying to remember the last activity and then starting next specific activity. This is the way to do it

change this

Intent intent = new Intent(Login.this, Class.forName(activity));
startActivity(intent);
finish();

to this-

  Intent intent = getIntent();
    String lastActivity = intent.getStringExtra("activity"); // lastActivity
    if (lastActivity.equalsIgnoreCase("FirstActivity")) {
        Intent intent = new Intent(Login.this, FirstActivity.class);
        startActivity(intent);
        finish();
    } else if (lastActivity.equalsIgnoreCase("SecondActivity")) {
        Intent intent = new Intent(Login.this, SecondActivity.class);
        startActivity(intent);
        finish();
    }

create common method to optimize your code

Upvotes: 1

David Wasser
David Wasser

Reputation: 95568

You need to provide the fully qualified class name. Instead of "FirstActivity" you need to pass "my.fully.qualified.class.name.FirstActivity" where you provide the fully qualified class name.

Upvotes: 0

Related Questions