I am using a simple function OnActivityResult, but it is not returning me desired results.
Please see my code and tell me where i am doing wrong.
public void OnClickFunction(View view)
{
Intent intent = new Intent(getApplicationContext(), Second.class);
startActivityForResult(intent, RESULT_OK);
/// My actions. . .
}
Then in the Second class, i have set Result like this:
Button.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
ValueOne = EditText.getText().toString().trim();
if (ValueOne.equals(String.valueOf(Answer)))
{
Toast.makeText(getApplicationContext(), "Correct Answer", 0).show();
Second.this.setResult(RESULT_OK, null);
Second.this.finish();
}
else
{
Toast.makeText(getApplicationContext(), "Wrong Answer", 0).show();
}
}
});
Now coming back to the first.class, from where the Intent was called:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// if (requestCode == RESULT_OK)
// {
if (resultCode == RESULT_OK)
{
///// MyActions. . .
}
// }
}
The debugger is not debugging this function, so the desired results are not coming.
Where i am doing wrong??
You have to destroy the second activity. Try pressing back button. I am able to see all the log messages in onActivityResult
First Activity
public class FirstActivity extends Activity {
/** Called when the activity is first created. */
int result = 100;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = new Intent(this,SecondActivity.class);
startActivityForResult(i, result);
}
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
Log.i("H", "RequestCode:" + requestCode);
Log.i("H", "ResultCode:" + resultCode );
}
}
SecondActivity
public class SecondActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setResult(RESULT_OK);
Log.i("S","Exiting Second Activity");
}
}
in Source Class:
int activity=1;
Intent i=new Intent(Sourceclass.this,destination.class);
startActivityForResult(i,activity);
In Destination class:
Intent i=new Intent();
setResult(RESULT_OK,i);
finish();
In OnActivityResult of Source Class:
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if(requestCode==1)
{
Log.e("check","check");
}
}
}
in my case: when use activityForResult, I add this flag :
addFlags(FLAG_ACTIVITY_NEW_TASK)
when delete this flag ,fixed
Related
I have a FragmentActivity that is starting another activity for result. When the called activity finishes, onActivityResult is not called. Does it make a difference that I am using a AppCompatActivity activity (which extends from FragmentActivity)? The documentation says that results will be returned to the calling fragment, and in this case it's not a fragment, it's an activity. Here is the code, very simple:
MainActivity:
public class SMSEmailActivityNew extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setup activity....
Intent i = new Intent(this, EulaActivity.class);
i.putExtra(Globals.keyFileName,Globals.FILE_EULA );
startActivityForResult(i,RESULT_OK);
}
//this method is never called
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//dowork .....
}
}
Called activity:
EulaActivity extends AppCompatActivity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set up activity ....
}
public void onClick(View v) {
Intent result = new Intent();
if (bPermissionGranted) {
setResult(Activity.RESULT_OK, result);
// Determine if EULA was accepted this time
getSharedPreferences().edit().putBoolean(Globals.KEY_EULA_ACCEPTED, true).apply();
} else {
setResult(Activity.RESULT_CANCELED, result);
}
finish();
}
}
According to documentation you need to pass requestId bigger or equal thant 0. In your case RESULT_OK is -1. Also RESULT_OK acts like result code, not like request code and startActivityForResult needs a request code.
Something like this startActivityForResult(intent, 0);
Also finish EulaActivity using finishActivity(yourPreviousRequestCode);, in this case 0.
Try this solution:-
MainActivity.java
//Define variable
public static int REQUEST_CODE = 233;
public class SMSEmailActivityNew extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setup activity....
Intent i = new Intent(this, EulaActivity.class);
i.putExtra(Globals.keyFileName,Globals.FILE_EULA );
startActivityForResult(i, REQUEST_CODE); //Change here
}
//this method is never called
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE) {
if(resultCode == RESULT_OK) {
if(data != null && data.hasExtra("MESSAGE")) {
String resStr = data.getStringExtra("MESSAGE");
Toast.makeText(MainActivity.this, resStr, Toast.LENGTH_SHORT).show();
}
}else if(resultCode == RESULT_CANCELED)
Toast.makeText(MainActivity.this, "Canceled", Toast.LENGTH_SHORT).show();
}
}
}
EulaActivity.java
EulaActivity extends AppCompatActivity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set up activity ....
}
public void onClick(View v) {
Intent result = new Intent();
result.putExtra("MESSAGE","Eula Accepted Set");
if (bPermissionGranted) {
setResult(Activity.RESULT_OK, result);
// Determine if EULA was accepted this time
getSharedPreferences().edit().putBoolean(Globals.KEY_EULA_ACCEPTED, true).apply();
} else {
setResult(Activity.RESULT_CANCELED, result);
}
finish();
}
}
I have a problem with the response of my activity. It is a module to react natively. I have this react class and I am trying to get result from my activity. I've tried catching the requestCode like this:
public CameraOpenerModule(ReactApplicationContext reactContext) {
super(reactContext);
getReactApplicationContext().addActivityEventListener(new ActivityEventListener() {
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.e("TEST", "listener start event");
if (requestCode == 9999) {
Log.e("TEST", "request code ");
}
}
});
}
and I open the class intent:
private void openCameraIntent(){
cameraIntent = new Intent(getReactApplicationContext(), CameraHelpActivity.class);
// cameraIntent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
cameraIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getReactApplicationContext().startActivityForResult(cameraIntent, 9999,null);
}
and another activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setResult(1111);
Log.i("ACTIVITY", "finish !!!!!");
finish();
}
Everything is ok. But I'm not catching the request code. Does anybody know why? Thanks
Add to MainActivity.java
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(mReactInstanceManager != null){
mReactInstanceManager.onActivityResult(requestCode,resultCode,data);
}
}
My application starts with InitActivity that checks for login status, calls LoginActivity accordingly before proceeding to some logic.
I know I can use the startAcivityForResult() and onActivityResult() to ensure LoginActivity completes before doing doSomeMainLogic(), but my if check throws a curve in it. If I do this:
public class InitActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!userIsLoggedIn) {
Intent intent = new Intent("com.example.myapp.LOGINACTIVITY");
startActivityForResult(intent,1);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
doSomeMainLogic();
}
}
}
}
how do I get doSomeMainLogic() to fire if the user is already logged in?
Thanks much.
public class InitActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!userIsLoggedIn) {
Intent intent = new Intent("com.example.myapp.LOGINACTIVITY");
startActivityForResult(intent,1);
}
else {
doSomeMainLogic(); // this part is added
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
doSomeMainLogic();
}
}
}
}
I need you help: I want to putExtra data to the previous activity before finishing the current activity.
Eg: Activity A start Activity B
When I finish Activity B I want in Activity A new data.
How I can do that?
Many thanks before
Android SDK explanation here, better SO question answer+example here.
Use startActivityforResult to open the activity B..then override onActivityResult(int, int, Intent) in your activity A..
Example:
public class MyActivity extends Activity {
...
static final int PICK_CONTACT_REQUEST = 0;
protected boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
// When the user center presses, let them pick a contact.
startActivityForResult(
new Intent(Intent.ACTION_PICK,
new Uri("content://contacts")),
PICK_CONTACT_REQUEST);
return true;
}
return false;
}
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == RESULT_OK) {
// A contact was picked. Here we will just display it
// to the user.
startActivity(new Intent(Intent.ACTION_VIEW, data));
}
}
}
}
check http://developer.android.com/reference/android/app/Activity.html
Use startActivityforResult to start Activity B. Implement override onActivityResult(int, int, Intent) method in Activity A and setResult in ActivityB.
Example:
public class ActivityA extends Activity {
static final int REQUEST_CODE = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.xyz);
DateBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivityForResult(new Intent(ActivityA.this,ActivityB.class), REQUEST_CODE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// A contact was picked. Here we will just display it
// to the user.
startActivity(new Intent(Intent.ACTION_VIEW, data));
}
}
}
}
public class ActivityB extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.xyz);
BackBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("DATA", "your string");
setResult(RESULT_OK, intent);
finish();
}
});
}
}
My application have 3 tabs. In this one tab have multiple activities(means this tab have navigation to the child tabs). I used the startActivityforResult() in one child activity. But control never goes to onActivityResult() method. How to implement this. please can anybody help me.
code
public class Activity_1 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
View contentView = LayoutInflater.from(getParent()).inflate(R.layout.static_search_filters, null);
setContentView(contentView);
states_tv = (TextView)findViewById(R.id.state);
states_tv.setOnClickListener(states_etListener);
}
private OnClickListener states_etListener = new View.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(getParent(), RB_CategoriesMList.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(intent,GET_SEL_STATES_LIST);
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
//I do some stuff here
}
}
//Activity_2
public class RB_CategoriesMList extends ListActivity
{
public Button sbtn;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.categories_list);
sbtn =(Button)findViewById(R.id.submit_categories);
sbtn.setOnClickListener(sbtn_listener);
}
private OnClickListener sbtn_listener = new View.OnClickListener()
{
public void onClick(View v)
{
Intent state_intent = getIntent();
state_intent.putExtra("selected_states", "");
setResult(RESULT_OK,state_intent);
finish();
}
};
}
Dont call
super.onActivityResult(requestCode, resultCode, data);
When you receive the onActivityResult, check if the request code is your and then do your stuff. In the other cases you should call the super.
So:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == GET_SEL_STATES_LIST) {
// Do your stuff
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
getParent().startActivityForResult(yourNewIntent, yourResult);