Sending data to activity - android

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

Related

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?
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.

Does the Intents get killed after i pass to another Intent Android

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();

How do you move user input from one activity to another activities listview

I am using an SQLite database and trying to put info into the database, but into a listview at the same time, is this possible?
yes, you pass variable to another activity as:
Intent intent = new Intent(this, AnotherActivity.class);
intent.putExtra('keyName', 'data');
startActivity(intent);
Then in AnotherActivity you acces them as
getStringExtra(String keyName)
Then you can populate your listview in new activity.
To pass values between activities, All you need to do is to bind the values to intent in source activity and retrieve them back at the target activity or somewhere else you need

How to send data from one activity to another by using getIntent() instead of usinf new Intent();

usually we send data from one activity to another by using:-
Intent i=new Intent("<action name>");
i.putExtras("name",data);
startActivity(i);
My question is can we send data from one activity to another by using:-
Intent i=this.getIntent();
i.putExtras("name",data);
setResult(Activity.RESULT_OK,i);
finish();
If yes please explain the concept.
Also,these two classes are in the different projects in Eclipse.My another question is, is it possible to send data through intent to another activity situated in another project??
So you're starting from Activity A and going to Activity B using startActivityForResult()
Now we're in Activity B and want to go back to Activity A:
Intent i = new Intent();
if(getIntent().getExtras() != null) i.putExtras(getIntent().getExtras());
setResult(Activity.RESULT_OK, i);
finish();
Doing something like that will allow you to pass back the extras from the calling Intent (if they exist), which would then be reachable from Activity A's onActivityResult() method.
Yes. You can do so, when you call another intent for a result. ie startActivityForResult(Intent,Request_Code);

Launching activity through intents

I am implementing a simple app. I need to start an activity based on the state of the Activity. Lets take i am using a button to start the activity.
1. If the activity is not started, I need to start XYZ activity.
2. If the XYZ activity is on focus, then i need to close the activity on the button press.
3. If the XYZ activity is not in focus (like onPause) state then, I need to change the button state.
Can you please help me in the flags that i need to use for starting the intent.
Is it possible to get the state of activity before I start that activity?
Try this
Intent intent = new Intent(currentActivity.this, callingActivity.class);
startActivity(intent);
You can use intent like this to call an activity
The button press will need to be captured by each activity separately, so just code the different responses into each different activity.
First off create a MAIN.java activity that is going to house your other activities. Like others have said you're going to have to code the button captures yourself because that should be common sense if you're trying to deal with intents. When you get that together though, you can start a new activity through intent like so:
// allocate new intent, initialized to the activity you wish to launch
Intent i = new Intent(this, ActivityToBeLaunched.class);
// put information into intent
i.putExtras("KeyName", value); // where "KeyName" is simply a reference string
// and 'value' can be anything from boolean - string.
// launch activity and wait for response
startActivityForResult(i, REQUEST_CODE);
Then within your ActivityToBeLaunched.java class you'll have an oncreate that will pull information from the intent like such:
// get intent
Intent i = this.getIntent();
// get information from intent
booleanVariable = i.getExtras().getBoolean("KeyName");
When you're done with this activity simply use;
// create intent
Intent i = new Intent();
// put information into result to send back to parent
i.putExtras("KeyName", value);
// set the result to be returned
setResult(i, ResultCode);
// finish child, return to parent with results
finish();

Categories

Resources