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?
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
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.
Related
I'm working on an android application and in the application I have a couple buttons that let user to pass to another activity. Now the way I'm doing the transitions between this Intents is like below:
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
With the above content I start an activity and from that activity I'm getting back to the MainActivity using this code:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
But i suspect this cause memory to be over used because I'm not actually getting back to the Main Activity that created when application started, I'm just creating another instance of MainActivity I guess. Is this really as i thought and if it is how can I get back to the activity that created in the beginning or if I can't do such thing how can I make app to let the previous activity go?
Passing an intent to startActivity() will create a new instance of the activity and add it to the front of the stack. So:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
is basically asking to create a new instance. If you want to go back to the activity just before the current one, call either:
finish();
Or,
super.onBackPressed();
In your solution you just have to press back button and you'll be back in first activity.
If you want to close it and after open new instance like you are doing in second activity just add
finish();
at the end of
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
You just need to call finish(); method
Intent intent = new Intent(this, DestinationActivity.class);
startActivity(intent);
finish();
So,I am trying to find out a way to send data to an activity WITHOUT starting it.
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(MESSAGE_KEY, message);
startActivity(intent);
Is there a way to do it without the(?):
startActivity(intent);
Well if you don't want to start an activity and pass the data then why to worry so much? Make the variable public and from the other activity class call the previous activity as follows and access that particular variable
((FirstActivity)getActivity()).theVariable
I an have activity called A in my project, there is a button that create new instance of the same activity. For example i want to do something like this:
intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(MainActivity.EXTRA_DATA, data);
startActivity(intent);
If i use this code the app crashes when i push the button.
I have found the solution. The activity use a object of another class called data, this class need to implement Serializable.
I am trying to follow the lesson here and now im stuck on "Building an Intent". I am quite confused for how to make this Intent and where to paste it. Can someone show me the step by step process on this tutorial? I am getting massive headaches now. Please I want to learn to do this.
Build an Intent
An Intent is an object that provides runtime binding between separate
components (such as two activities). The Intent represents an app’s
"intent to do something." You can use intents for a wide variety of
tasks, but most often they’re used to start another activity.
Inside the sendMessage() method, create an Intent to start an activity
called DisplayMessageActivity:
Intent intent = new Intent(this, DisplayMessageActivity.class); The
constructor used here takes two parameters:
A Context as its first parameter (this is used because the Activity
class is a subclass of Context) The Class of the app component to
which the system should deliver the Intent (in this case, the activity
that should be started)
As the tutorial says, you need to add the line of code that creates an new instance of the Intent class. You will use this instance later to tell the OS to launch another activity or a service. In this particular example, the Intent you are building will direct the OS to launch the DisplayMessageActivity.
To do this step properly, you need to modify the sendMessage method that you have added in the previous step of the tutorial. The final method should look something like this:
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
}
After creating the Intent, the code will take the content of the editText control in the current activity, assign it to the message variable, and then add it as an additional parameter to the intent, so that the target DisplayMessageActivity activity can do something with it.
Don't worry about the DisplayMessageActivity yet. It will be added in a later step.
How do i create Intent and to where to put the codes?
You want to open the activity using intent then you can write your code in this method.
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
I have seen the following two examples of starting activities in Android:
Example 1
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);
Example 2
// Calling activity
NextActivity.show(this)
// In the called activity
static void show(Context context) {
final Intent intent = new Intent(context, NextActivity.class);
context.startActivity(intent);
}
It seems the obvious difference between the two examples is that you attach the logic of how an activity is created to the implementation of the activity. Are there any other key differences? (e.g. is the calling activity told to wait until the called activity finishes in one case, but not in the other, etc.)
I see no difference to your 2 methods, other than the 2 lines of code in your first method just happen to be located in a static method that just happens to be located in the 2nd activity's class.
The actual lines of code that are being executed to start the activity are identical. Thus the behavior of the 2 methods will be identical.
Also, the code could be shortened to
context.startActivity(new Intent (context, NextActivity.class));
Only reason to create an instance of Intent as a field is if you need to set flags or add extras, etc.