in own service i want to send data with intent but i am getting null from Activity.I have read about document from this solution but i can not resolve the problem.
SERVICE :
public void notifyTest(int unread){
Intent i = new Intent();
i.setClass(this, YourDialog.class);
i.putExtra("data", unread);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity ( i );
}
get data from Activity:
Intent intent = getIntent ();
String count = intent.getStringExtra("data");
i can not use BroadCostReceiver to resolve problem.
I think this error happened because your data is int, but you want to receive it as String.
Try to change it like this :
public void notifyTest(int unread){
Intent i = new Intent();
i.setClass(this, YourDialog.class);
i.putExtra("data", unread + ""); //convert to STRING
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity ( i );
}
Try this way,hope this will help you to solve your problem.
unread is int then :
getIntent().getIntExtra("data",0);
unread is String then :
getIntent().getStringExtra("data");
Use this to "put"
Intent i = new Intent(FirstScreen.this, SecondScreen.class);
String keyIdentifer = null;
i.putExtra("STRING_I_NEED", strName);
Then, to retrieve the value try something like:
String newString;
if (savedInstanceState == null) {
extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("STRING_I_NEED");
}
} else {
newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}
Related
Is there any way to check if an extra has been passed when starting an Activity?
I would like to do something like (on the onCreate() in the Activity):
Bundle extras = getIntent().getExtras();
String extraStr = extras.getString("extra");
if (extraStr == null) {
extraStr = "extra not set";
}
But this is throwing a java.lang.NullPointerException.
Thank you.
Use the Intent.hasExtra(String name) to check if an extra with name was passed in the intent.
Example:
Intent intent = getIntent();
if (intent.hasExtra("bookUrl")) {
bookUrl = b.getString("bookUrl");
} else {
// Do something else
}
Also, use Intent.getStringExtra(String name) directly on the intent to handle the NullPointerException if no extras were passed.
Well, I had similiar problem. in my case the null point exception was happen when I checked if my bundle.getString() was equall to null.
here is how IN MY CASE I solved it:
Intent intent = getIntent();
if(intent.hasExtra("nomeUsuario")){
bd = getIntent().getExtras();
if(!bd.getString("nomeUsuario").equals(null)){
nomeUsuario = bd.getString("nomeUsuario");
}
}
if (this.getIntent().getExtras() != null && this.getIntent().getExtras().containsKey("yourKey")) {
// intent is not null and your key is not null
}
I think you need to check when extras != null
Bundle extras = getIntent().getExtras();
if (extras != null) {
String extraStr = extras.getString("extra");
}else {
extraStr = "extra not set";
}
I would use this solution in your case.
String extraStr;
try {
extraStr = getIntent().getExtras().getString("extra");
} catch (NullPointerException e ) {
extraStr = "something_else";
}
Working Code
If you want to check first Intent have no extra
Intent intent = getIntent();
if (intent.getExtras() == null){
startActivity(new Intent(Splash.this, Main.class));
overridePendingTransition(R.anim.enter, R.anim.exit);
finish();
}else {
if (intent.hasExtra("type")) {
String type = getIntent().getStringExtra("type");
switch (type){
case "showRateUsDialog":
Intent i = new Intent(Splash.this, Main.class);
i.putExtra("type", "showRateUsDialog");
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
overridePendingTransition(R.anim.enter, R.anim.exit);
finish();
break;
case "refer":
Intent i2 = new Intent(Splash.this, Refer.class);
i2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i2);
overridePendingTransition(R.anim.enter, R.anim.exit);
finish();
break;
default:
startActivity(new Intent(Splash.this, Main.class));
overridePendingTransition(R.anim.enter, R.anim.exit);
finish();
}
}
}
}
I'm not sure what is going on here. I replaced a network call from SpringFramework to Volley, and now my Intent doesn't work. Nothing has changed on the successful return, and the Volley request returns the correct information, however, when the Intent is done, it comes back with two different returns.
Here's the working version:
NetworkEngine networkEngine=new NetworkEngine(getApplicationContext());
verifyResponse=networkEngine.sendHTTPPOSTRequest(NetworkConstants.Url_pattern_verify, postJson.toString());
if(verifyResponse.getStatusCode().value()==200){
JSONObject verifyJson = new JSONObject(new String(verifyResponse.getBody()));
result="success";
authcode=verifyJson.getString("auth_code");
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
returnIntent.putExtra("authcode", verifyJson.getString("auth_code"));
Log.i("verifyJson", verifyJson.toString());
Bundle extras = getIntent().getExtras();
if (extras != null){
accountId = extras.getString("accountId");
returnIntent.putExtra("accountId", accountId);
}
setResult(RESULT_OK, returnIntent);
finish();
}
else if (verifyResponse.getStatusCode().value()==401){
tryAgain();
}
I logged the result from the returnIntent.putExtra, and RESULT_FAILED
I/return﹕ success
I/failed﹕ 2
Now here's the none working one.
Globals.POST(url, headers, params, postJson.toString(), new Globals.VolleyCallback() {
#Override
public void onSuccess(String result) {
try {
JSONObject verifyJson = new JSONObject(result);
result="success";
Log.i("verifyJson", verifyJson.toString());
authcode=verifyJson.getString("auth_code");
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
returnIntent.putExtra("authcode", verifyJson.getString("auth_code"));
Bundle extras = getIntent().getExtras();
if (extras != null){
accountId = extras.getString("accountId");
returnIntent.putExtra("accountId", accountId);
}
Log.i("Intent", result);
setResult(RESULT_OK, returnIntent);
finish();
} catch(Exception e){
e.getStackTrace();
Log.i("Catch", e.getMessage());
}
}
#Override
public void onFail(String result) {
tryAgain();
}
With the same returns logged
I/return﹕ [ 08-14 09:44:09.601 20541:20541 I/failed ]
2
Came in just like that, with no line break. I have no idea what's going on. Nothing besides what library is making the API call has changed. The returned info from the API is exactly the same too.
I am trying to make a login activity which proceeds to another activity by clicking. but its giving me error. here is the code....
public void onButtonClick(View v) {
if(v.getid() == R.id.Blogin)
{
EditText a=(EditText) findViewById(R.id.TFUserName);
String str= a.getText().toString();
Intent i = new Intent(this, SignIn.class);
i.putExtra("Username",str);
startActivity(i);
}
}
its giving me error in view and getid.. any one can help me in solving this error?
Try by this code .
First Activity:-
Intent i = new Intent(FirstScreen.this, SecondScreen.class);
String keyIdentifer = null;
i.putExtra("STRING_I_NEED", strName);
Second Activity :-
String newString;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("STRING_I_NEED");
}
} else {
newString= (String)savedInstanceState.getSerializable("STRING_I_NEED");
}
complete code ..
or
you can use shared Preference to save values of user Login.
Hi there I want to call an Activity an decide which is the next activity to start after the Dialog finished like:
Intent dialog_intent = new Intent(_parent, MyActivityA.class);
dialog_intent.putExtra(MyActivityA.EXTRA_PARENT, MyActivityB.class);
I am getting the extra like this in the Oncreate of MyActivityA:
Type _parent = null; // this is a class variable
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Set<String> keys = bundle.keySet();
Iterator<String> it = keys.iterator();
while (it.hasNext())
{
String key = it.next();
if(key.equals(EXTRA_PARENT))
_parent = (Type)bundle.get(EXTRA_PARENT);
}
}
in the finishDialog method I do this :
public void finishDialog(View v)
{
try
{
Intent intent = null;
if(_parent != null && _parent instanceof Activity)
{
intent = new Intent(this, _parent.getClass());
}
else
{
intent = new Intent(this, DefaultActivity.class);
}
if(intent != null)
{
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
finish();
}
2 Questions:
Why does _parent instaneof Activity not work. I think MyActivity is a Type which inherits of Activity
Trying to start the activity does not work. But if i put in
new Intent(this,MyActivityB.class);
it works! What am I doing wrong. Is there any other way to do this
You can simply pass integer value and map it to corresponding activity.
Intent dialog_intent = new Intent(_parent, MyActivityA.class);
dialog_intent.putExtra("EXTRA_PARENT", 1);
Now in your activity get this EXTRA_PARENT and call activity
Bundle extras = getIntent().getExtras();
int extraParent = extras.getInt("EXTRA_PARENT");
if(extraParent == 1)
{
Intent dialog_intent = new Intent(_parent, MyActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
}
This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 8 years ago.
My app has 2 activites.
The first activity is just a simple form where a user enters course information(class title, professor..etc.) the first activity passes the data which is supposed to be stored in a list.
In the second activity. The problem is that only the first course gets stored in the list, after the first time nothing new gets added to the second activity.
How can i do this ?
In your first activity :
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("login", jObj.getString(KEY_LOGIN));
intent.putExtra("mdp", jObj.getString(KEY_MDP));
intent.putExtra("prenom", jObj.getString(KEY_PRENOM));
intent.putExtra("nom", jObj.getString(KEY_NOM));
intent.putExtra("mail", jObj.getString(KEY_MAIL));
intent.putExtra("tel", jObj.getString(KEY_TEL));
startActivity(intent);
In your second activity :
Intent intent = getIntent();
if (intent != null) {
login = intent.getStringExtra("login");
mdp = intent.getStringExtra("mdp");
items.add(intent.getStringExtra("login"));
items.add(intent.getStringExtra("prenom"));
items.add(intent.getStringExtra("nom"));
items.add(intent.getStringExtra("mail"));
items.add(intent.getStringExtra("tel"));
}
Generally you can pass data from one Activity to another with an Intent like this:
In your first Activity:
// Create your Intent
Intent intent = new Intent(context, TargetActivity.class);
// Now you can add extras to the intent, you identify extras with a String key
intent.putExtra("text", someString);
intent.putExtra("amount", someInteger);
// Then you start your Activity with this Intent
startActivity(intent);
In your second Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Only get data from Intent when the Activity is new
if(savedInstanceState == null) {
Intent intent = getIntent();
// Now you read the values from the Intent
String someString = intent.getStringExtra("text");
int someInteger = intent.getIntExtra("amount", 0);
}
}
hey its simple look at the code
for sending
Intent i = new Intent(getApplicationContext(), Confirmation.class)
i.putExtra("name",etName.getText().toString()));
i.putExtra("pass",etPass.getText().toString());
startActivity(i);
for recieving in next activity
Bundle extras = getIntent().getExtras();
String strEmployeeID="";
if (extras != null)
{
String value = extras.getString("name");
String value1 = extras.getString("pass");
//
Toast.makeText(getBaseContext(), value, Toast.LENGTH_LONG).show();
strEmployeeID = value;
strEmployeePass = value1;
}
just simply do it like that:
passing data from 1st activity to 2nd activity:
Intent intent = new Intent(yourafirstctivity.this,Yoursecondactivity.class);
intent.putExtra("yourkey", yourvalue);
intent.putExtra("yourkey", yourvalue);
intent.putExtra("yourkey", yourvalue);
startActivity(intent);
and get the data from 1st activity to 2nd activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.yourlayout);
String yourstring = getIntent().getExtras().getString("yourkey");
String yourstring = getIntent().getExtras().getString("yourkey");
String yourstring = getIntent().getExtras().getString("yourkey");
}
if you want to send data between two Activities .You must call following one,
startActivityForResult(getcontext(), SecondActivity.class);
On calling this,Activity starts the second.In turn, second response to this intent and reply back with necessary data.
public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
if (mParent == null) {
Instrumentation.ActivityResult ar =
mInstrumentation.execStartActivity(
this, mMainThread.getApplicationThread(), mToken, this,
intent, requestCode, options);
if (ar != null) {
mMainThread.sendActivityResult(
mToken, mEmbeddedID, requestCode, ar.getResultCode(),
ar.getResultData());
}
if (requestCode >= 0) {
mStartedActivity = true;
}
} else {
if (options != null) {
mParent.startActivityFromChild(this, intent, requestCode, options);
} else {
// Note we want to go through this method for compatibility with
// existing applications that may have overridden it.
mParent.startActivityFromChild(this, intent, requestCode);
}
}
}
Following one you must call this on the First Activity (from where the intent called).It helps to recieves the reply data from second Activity.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(!isFeatureInstalled(getContext()))
return;
if(requestCode == GET_REMINDER_DETAILS && resultCode == Activity.RESULT_OK)
{
mFilled = true;
fillDataFromIntent(data);
checkALertRequired.setChecked(true);
}
}