From some Fragment I call an Activity like this:
private void setEmailChangeListener() {
emailChangeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), EmailChangeActivity.class);
startActivityForResult(intent, 1);
}
});
}
In that Activity I make some changes to e-mail in database and then update info in local database (updated e-mail and update time and date). Then I call setResult:
if (!error) {
JSONObject user = jObj.getJSONObject("user");
String updatedAt = user.getString("updated_at");
String userUniqueId = db.getUserUniqueId();
db.updateUpdatedAt(updatedAt, userUniqueId);
Intent returnIntent = new Intent();
setResult(Activity.RESULT_OK, returnIntent);
finish();
} else {
String errorMsg = jObj.getString("error_msg");
showSnackbarInfo(errorMsg, R.color.snackbar_error);
}
In Activity with opened Fragment I want to update info in Fragment by detach and attach in order to refrsh it. I do it in onActivityResult like this:
// Result From Other Activities
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == 1) {
Log.i("result", "reqCode = 1, zmieniam adres email w nav draw i odswierzam fragment");
navHeaderEmail.setText(db.getEmailAdress());
Fragment fragment = getSupportFragmentManager().findFragmentByTag(
TAG_FRAGMENT_ACCOUNT_SETTINGS);
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.detach(fragment);
fragmentTransaction.attach(fragment);
fragmentTransaction.commit();
showSnackbarInfo(getString(R.string.inf_email_change_success),
R.color.snackbar_success);
}
}
After calling setResult nothing happens just like I didn't set the result properly or something else. Could you help me with this?
Here is schema how it should work:
Edit:
Here is my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nazwamarki.myapplication">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:name=".app.AppController"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/application_name"
android:theme="#style/AppTheme">
<!-- Home Activity -->
<activity
android:name=".HomeActivity"
android:label="#string/application_name"
android:theme="#style/AppTheme.Normal">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Login Activity -->
<activity
android:name=".LoginActivity"
android:label="#string/login_activity_name"
android:parentActivityName=".HomeActivity"
android:theme="#style/AppTheme.Logreg">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.nazwamarki.myapplication.HomeActivity" />
</activity>
<!-- Sign Up Activity -->
<activity
android:name=".SignUpActivity"
android:label="#string/register_activity_name"
android:parentActivityName=".HomeActivity"
android:theme="#style/AppTheme.Logreg">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.nazwamarki.myapplication.HomeActivity" />
</activity>
<!-- Email Change Activity -->
<activity
android:name=".EmailChangeActivity"
android:label="#string/change_email_activity_name"
android:theme="#style/AppTheme.Normal">
</activity>
<!-- Password Change Activity -->
<activity
android:name=".PasswordChangeActivity"
android:label="#string/change_password_activity_name"
android:theme="#style/AppTheme.Normal">
</activity>
<!-- Recipe Activity -->
<activity
android:name=".RecipeActivity"
android:label="#string/recipe_activity_name"
android:parentActivityName=".HomeActivity"
android:theme="#style/AppTheme.Normal">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.nazwamarki.myapplication.HomeActivity" />
</activity>
</application>
</manifest>
can you try
getActivity().startActivityForResult(intent, 1);
or override onActivityResult in the fragment it self
I had the problem in reverse I was trying to use onActivityResult inside my fragment but sometimes it went to the parent activity so I ended up using the first line and handling it in the parent actvity
Related
I am developing an Android app. In my app, I am integrating Facebook Login. I have done developing Facebook Login once before. When I develop this time, Facebook callback functions are not called. I cannot check the error as well. I have no idea with what is wrong.
I installed Facebook SDK using Gradle
compile 'com.facebook.android:facebook-android-sdk:4.1.0'
Then I generated key hash and set in the Facebook developer settings as follows:
This is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tonightfootballreport.com.tfr" >
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:name="tonightfootballreport.model.TfrApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="#string/facebook_app_id"/>
<activity
android:configChanges="orientation|screenSize"
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:configChanges="orientation|screenSize"
android:name=".FacebookConfigActivity"
android:label="Facebook"
android:theme="#style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="tonightfootballreport.com.tfr.FacebookConfigActivity"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:theme="#android:style/Theme.Translucent.NoTitleBar"
android:label="#string/app_name" />
<provider android:authorities="com.facebook.app.FacebookContentProvider1817380365215100"
android:name="com.facebook.FacebookContentProvider"
android:exported="true" />
</application>
</manifest>
As you can see Facebook Login is not in main activity and so I set FacebookConfigActivity in Developer Settings.
Then when I click a button in MainActivity, FacebookConfigActivity will be opened and perform Facebook Login. Below is the code.
public class FacebookConfigActivity extends AppCompatActivity {
private CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext(), AppConfig.FACEBOOK_REQUEST_CODE);
AppEventsLogger.activateApp(this);
setContentView(R.layout.activity_facebook);
setUpCallBack();
loginInToFacebook();
}
private void loginInToFacebook()
{
LoginManager.getInstance().logInWithReadPermissions(FacebookConfigActivity.this,Arrays.asList("public_profile"));
}
private void setUpCallBack()
{
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
//show share alert
//then share
Log.i("LOGIN_SUCCESS","LOGIN_SUCCESS");
}
#Override
public void onCancel() {
Toast.makeText(getBaseContext(), "Operation canceled", Toast.LENGTH_SHORT).show();
finish();
}
#Override
public void onError(FacebookException error) {
Toast.makeText(getBaseContext(), "Error in connecting to Facebook", Toast.LENGTH_SHORT).show();
finish();
}
});
}
}
As you can see, it is just simple Facebook Login. So when I click login button, I enter credentials to the Facebook Login form you can see below.
Then after I click OK in the next step, FacebookConfigActivity is displayed and no callback function is called. If I click cancel, Facebook Cancel callback is not called as well. What is the possible error?
You forgot to put listener in onActivityResult()
put below code in onActivityResult method of activity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
I have three activity like this :
+ LoginPage : display only one time when app installed
+ MainActivity : Home screen of app
+ TextNoteActivity: Sub activity is called by startActivityForResult from MainActivity
I don't know why when i click up home button from TextNoteActivity. The app will close.
The following is my code.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lilprogramming.bossnote">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name="activity.MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"
>
</activity>
<activity
android:name="activity.LoginPage"
android:label="#string/app_name"
android:noHistory="true"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="activity.TextNoteActivity"
android:parentActivityName="activity.MainActivity"
>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="activity.MainActivity" />
</activity>
<activity
android:name="activity.AddTagActivity"
android:label="#string/title_activity_add_tag"
android:theme="#style/AppTheme">
</activity>
</application>
In MainActivity :
Intent i = new Intent(this, TextNoteActivity.class);
startActivityForResult(i, REQUEST_ADDTEXTNOTE);
In TextNoteActivity :
toolbar = (Toolbar) findViewById(R.id.addTextNote_toolbar);
toolbar.setTitle("");
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addTextNote();
Toast.makeText(TextNoteActivity.this, "Back then", Toast.LENGTH_SHORT).show();
finish();
}
});
Update
private void addTextNote() {
String title = etTitle.getText().toString();
String content = etContent.getText().toString();
if (!title.isEmpty() && !content.isEmpty()) {
TextNote textNote = new TextNote(title, 0, content);
database.addTextNote(textNote, null);
setResult(RESULT_OK);
}
else {
setResult(Activity.RESULT_CANCELED);
}
}
That is expected behavior
There is no reason for your activity to return a result when the user pushes the Home button.
If you need to catch the Home button event
You could override onPause() and finish() the activity.
DISCLAIMER
But I'm afraid this is not a good app-behavior
Because the user won't expect the app state to change when he pushes the Home button.
i want to open a excel file when a user selects it from listview. and i want to know whether the opened file is edited or not . when ever user edits a file i want to upload that file to server how can i do that thing. i tried with the following code but the "onactivityresult" method is not getting called .
Code:
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.m.media"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17"
/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MyActivity" >
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<action android:name="android.intent.action.SEND"/>
<action android:name="android.intent.action.EDIT"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- <data android:mimeType="image/*" />
<data android:mimeType="video/*" /> -->
<data android:mimeType="*/*" />
</intent-filter>
</activity>
</application>
</manifest>
MyActivity:
listviewv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item=filenames.get(position);
String ext = item.substring(item.lastIndexOf(".")+1);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String type = mime.getMimeTypeFromExtension(ext);
File videoFile2Play = new File(filepath.get(position));
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_EDIT);
i.setDataAndType(Uri.fromFile(videoFile2Play), type);
i.putExtra("finishActivityOnSaveCompleted", true);
startActivityForResult(i, EDIT_CONTACT_RESULT);
// Toast.makeText(getBaseContext(), filepath.get(position), Toast.LENGTH_LONG).show();
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == EDIT_CONTACT_RESULT) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
Toast.makeText(getBaseContext(), "edited", Toast.LENGTH_LONG).show();
}
}
}
but the onactivityresult method is not getting called when i save the edit changes. how can i get notified when an edit occurred in opened file. please help me in solving this problem.
i am using the android vesion-4.0.3.
Based on the fact that you are getting onActivityResult() called with RESULT_CANCELED, I'm assuming that whatever activity is launched when you invoke the EDIT action is running in another task. In this case you cannot rely on getting a useful result back from startActivityOnResult().
To see if the file has been modified, you probably need to use a different approach. Try implementing a FileObserver to watch the file that you are editing and listen for the MODIFY event.
I am trying to implement a QR code reader in my android app. I have followed these steps:
Created a sample library project from the zxing.zip , /android and /core
Added the created library to my app
My code is as below :
public class main extends Activity {
/** Called when the activity is first created. */
Button scanButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
scanButton = (Button) findViewById(R.id.button1);
scanButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(
"com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
The scanner doesn't start and I get this error:
05-08 14:12:03.313: ERROR/AndroidRuntime(718): Caused by: java.lang.ClassNotFoundException: com.google.zxing.client.android.CaptureActivity in loader dalvik.system.PathClassLoader[/data/app/com.scanner.demo-2.apk]
The manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.scanner.demo" android:versionCode="1" android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".main" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.google.zxing.client.android.CaptureActivity"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="com.google.zxing.client.android.SCAN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Why do you put this in your Manifest?
<activity
android:name="com.google.zxing.client.android.CaptureActivity"
...
</activity>
Are you calling internal activity or the App from Zxing?
This is quite confused. You seem to be wanting to integrate by Intent, which is what the first half does. But then you seem to have copied our Manifest. Why? Please remove that. It's not needed, causes your error, and if you leave it in, interferes with our app.
This is all you need: https://code.google.com/p/zxing/wiki/ScanningViaIntent
KitActivity:
/** in KitActivity, handler invoked after successful transmission **/
private final Handler txHandle = new Handler() {
#Override
public void handleMessage(Message msg) {
boolean success = msg.getData().getBoolean("success");
dismissDialog(DIALOG_TX_PROGRESS);
if(success) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putInt("previous_scale", mScaleSpn.getSelectedItemPosition());
editor.commit();
//clearFields();
//showDialog(DIALOG_ETX);
KitActivity.this.setResult(0);
KitActivity.this.finish();
} else {
removeDialog(DIALOG_FAIL);
showDialog(DIALOG_FAIL);
}
}
};
MainActivity:
/** in the MainActivity **/
public void startCreateKit() {
Intent i = new Intent(MainActivity.this, KitActivity.class);
startActivityForResult(i,0);
}
protected void onActivityResult(int reqCode, int resCode) {
if(reqCode==0) {
if(resCode==0) {
//we good, perform sync
showDialog(DIALOG_TX_PROGRESS);
Toast.makeText(this, "Performing Auto Sync", Toast.LENGTH_LONG).show();
updateKits();
} else {
//uh oh
}
}
}
createKitBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Toast.makeText(context, "NEW KIT", Toast.LENGTH_SHORT).show();
startCreateKit();
}
});
onActivityResult is never called in MainActivity. this is pretty much by the book. what's the issue?
stuff i've tried:
- using Activity.RESULT_OK for the result code (which translates to -1);
- removing the setResult() and finish() calls from the handler and calling an outside method to invoke them.
i don't see anything wrong. here's the manifest, nothing awry here:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.conceptualsystems.kitmobile"
android:versionCode="8"
android:versionName="#string/version">
<application android:label="#string/app_name" android:icon="#drawable/icon" android:debuggable="true">
<activity android:name="MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="ShipActivity"
android:label="Ship Kits"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
</activity>
<activity android:name="KitActivity"
android:label="Kit Entry"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
</activity>
<activity android:name="ColorActivity"
android:label="Color Selection"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
what gives?
in the activity you start (KitActivity.class) on success you do this
Intent intent = this.getIntent();
intent.putExtra("SOMETHING", "EXTRAS");
this.setResult(RESULT_OK, intent);
finish();
else you put RESULT_CANCELED instead of RESULT_OK
replaced:
protected void onActivityResult(int reqCode, int resCode)
with:
protected void onActivityResult(int reqCode, int resCode, Intent intent)
amazing what happens when you review the documentation... -_-