If statement use in calling onActivityResult() - android

I am curious to know the difference between the following two methods. Does it matter which one I use, or can they be used according to preference?
Here is the first example:
#Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK){
return;
}
if (requestCode == REQUEST_DATA){
//Do stuff
}
}
Here is the second one:
#Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_DATA){
//Do stuff
}
}

Logically both statements are identical.
The second statement makes the code more readable and understandable, so I always prefer the same. But the condition statement is too complex then nested if is more preferable. Anyway it depends on the coding standard that you follow.

Related

Pass onActivityResult() data to the same Fragment which is not yet ready

I am using a Fragment to start a new Activity using startActivityForResult(), I am getting the result (Bundle) in onActivityResult() method.Since onActivityResult() called before onResume().I want to make sure, I keep/save the Bundle properly so that when Fragment's onResume() gets called, I get the kept/saved result to perform further action.
What are the different ways to achieve this. I tried using getArguments()/setArguments(), but that seems to be not the right way to achieve this.
Try this
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
mResultBundle = data.getExtras(); //mResultBundle is in fragment
//scope
}
}
}
#Override
protected void onResume(){
super.onResume();
if(mResultBundle != null){
// process saved bundle from activity result here
// don't forget to set it back to null once you are done
mResultBundle = null;
}
}

React Native(Android) Scan QRCode

I wanna write a native module for to scan barcode but it too hard for me to figure out a way to handle the result without adding a method to the MainActivity. It's not a good idea to modify the MainActivity such heavily because it's no easy job for application developers who writes javascript to use the module.
For example, if I use ZXing Android Embedded: https://github.com/journeyapps/zxing-android-embedded, I have to add a method to MainActivity to handle the result.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
} else {
// This is important, otherwise the result will not be passed to the fragment
super.onActivityResult(requestCode, resultCode, data);
}
}
That makes an issue that any one who uses this module has to modify their MainActivity, which means the module is hard to use. So, any ideas to work it out?
You can just use react-native-rn-zxing:
npm i react-native-rn-zxing
then link it :
react-native link react-native-rn-zxing
And enjoy

Error: Activity result fragment index out of range: 0x2fffe

When im trying to delete a show with an AsyncTask. I want to call finish() after the AsyncTask has been completed and return an Intent with the result.
from the activity:
new DeleteShowTask().execute();
Intent intent = new Intent(SeasonActivity.this, FragmentShows.class); // I'm not sure if this works
intent.putExtra("tvdbid", tvdbId);
setResult(DELETECODE, intent);
finish();
then in the fragment i have this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("RESULTCODE", resultCode + "");
Log.d("REQUEST CODE", requestCode +"");
if (resultCode == SeasonActivity.DELETECODE)
{
if (requestCode == SeasonActivity.SHOW)
{
String tvdbid = data.getStringExtra("tvdbid");
for (int i = 0; i < adapter.getCount(); i++) {
SickbeardSerie serie = adapter.getItem(i);
if (serie.getTvdbId().equals(tvdbid))
{
adapter.remove(serie);
adapter.notifyDataSetChanged();
}
}
}
}
}
But it seems like it doenst run through this onAcitivityResult().
I have logged the onActivityResult() as you see but i dont get any logs.
Only thing i get is: 10-19 16:21:44.631: W/FragmentActivity(27672): Activity result fragment index out of range: 0x2fffe
I fixed it for now by using a work around.
I now reload my AsyncTask in the onResume() instead of just removing an item from the arraylist and calling adapter.notifyDataSetChanged(). But its not the best solution.
if you using a Fragment in a another Fragment. You should be call
getParentFragment().startActivityForResult(i, SELECT_PICTURE);

Facebook requestCodes

I have an Activity that should handle results from both the Facebook SDK, and from other custom Activities.
Where can I find the requestCodes used by the Facebook SDK, in order to not use the same for my Activities?
I should be able to tell them apart in the onActivityResult using their requestCode so they need to be unique.
Pass the request code in the sdkInitialize call
FacebookSdk.sdkInitialize(context, 1200);
Then
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (FacebookSdk.isFacebookRequestCode(requestCode)) {
//Facebook activity result
//Do your stuff here
//Further you can also check if it's login or Share etc by using
//CallbackManagerImpl as explained by rajath's answer here
if (requestCode == CallbackManagerImpl.RequestCodeOffset.Login.toRequestCode()) {
//login
}
else if (requestCode == CallbackManagerImpl.RequestCodeOffset.Share.toRequestCode()){
//share
}
}
From the docs
isFacebookRequestCode(int)
Returns true if the request code is within the range used by Facebook SDK requests. This does not include request codes that you explicitly set on the dialogs, buttons or LoginManager. The range of request codes that the SDK uses starts at the callbackRequestCodeOffset and continues for the next 100 values.
sdkInitialize(Context, int)
This function initializes the Facebook SDK, the behavior of Facebook SDK functions are undetermined if this function is not called. It should be called as early as possible.
public static synchronized void sdkInitialize(Context applicationContext, int callbackRequestCodeOffset)
applicationContext The application context
callbackRequestCodeOffset The request code offset that Facebook activities will be called with. Please do not use the range between the value you set and another 100 entries after it in your other requests.
Go to CallbackManagerImpl.RequestCodeOffset. I personally used a piece of code like this to prevent unwanted behaviour.
if (requestCode == CallbackManagerImpl.RequestCodeOffset.Login.toRequestCode()) {
callbackManager.onActivityResult(requestCode, resultCode, data);
}
Try this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
//Log.iClassMethod();
switch(requestCode)
{
case 1:
if (resultCode == Activity.RESULT_OK)
{
// do something ...
}
break;
case ...:
if (resultCode == Activity.RESULT_OK)
{
// do something ...
}
break;
case Session.DEFAULT_AUTHORIZE_ACTIVITY_CODE:
{
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
Log.i("Facebook");
}
break;
}
}
Offering an alternative if you're using FB login via LoginButton
Set request code of login button
Use the request code to differentiate activity
private LoginButton mFacebookLoginButton;
private static int RC_FB_SIGN_IN;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
mFacebookLoginButton = (LoginButton) findByViewId(R.id.fb_login_button);
mFacebookLoginButton.registerCallback(...)
RC_FB_SIGN_IN = mFacebookLoginButton.getRequestCode();
...
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_GP_SIGN_IN) {
...
} else if (requestCode == RC_FB_SIGN_IN) {
Log.i(TAG, "Doing my facebook usual things");
mFacebookCallbackManager.onActivityResult(requestCode, resultCode, data);
}
}
You can set your own request codes to disambiguate. All the OpenRequest and NewPermissionsRequest classes take a requestCode parameter:
setRequestCode
Adding to Apetroaei Andrei's answer, there are other options available at Facebook SDK's CallbackManagerImpl class:
public enum RequestCodeOffset {
Login(0),
Share(1),
Message(2),
Like(3),
GameRequest(4),
AppGroupCreate(5),
AppGroupJoin(6),
AppInvite(7),
;
These can be accessed by the foll. code:
if (requestCode == CallbackManagerImpl.RequestCodeOffset.Share.toRequestCode()) {
callbackManager.onActivityResult(requestCode, resultCode, data);
}
in the lates sdk, (4.4.1), you can manually set the request code by modifying two files in the facebook library:
1) LoginClient.java
public static int getLoginRequestCode() {
//return CallbackManagerImpl.RequestCodeOffset.Login.toRequestCode();
return [your request code here];
}
2)LoginManager.java
public void registerCallback(
...
((CallbackManagerImpl) callbackManager).registerCallback([your request code here],
new CallbackManagerImpl.Callback() {
#Override
...
}
);
}

notifyDataSetChanged() doesn't update ListView

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
deserializeQuotes();
quotesAdapter.notifyDataSetChanged();
}
}
My array clearly has been updated, and I can see the change when my app starts, but why doesn't it update in this method? The code enters this method.
this.quotesAdapter = new QuoteAdapter(this, R.layout.mainrow, quotes);
quotesAdapter.notifyDataSetChanged();
listView.setAdapter(quotesAdapter);
Works, but why do I have to create a new adapter? Why can't I use my existing one?

Categories

Resources