onActivtyResult is called to early [SOLVED]

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

onActivtyResult is called to early [SOLVED]

Postby BlackDragon » Fri May 08, 2009 5:47 pm

Hello.

I have got a main activity, where i have overridden the "onActivityResult" function.
In this activity a function is called from a context menu, where i start a dialog-like subactivity.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. // in the called function
  2. ...
  3.   Intent dialog = new Intent(this, DirectorySelectionDialog.class);
  4.      
  5.   Bundle b = new Bundle();
  6.   b.putString(DirectorySelectionDialog.INITIAL_DIRECTORY, it.getFile().getPath());
  7.   dialog.putExtras(b);
  8.      
  9.   startActivityForResult(dialog, DIRECTORY_SELECTION_DIALOG_CODE);
  10. ...
  11.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


The overridden "onActivityResult" function in the main activity looks like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.     protected void onActivityResult(int requestCode, int resultCode,
  3.         Intent data)
  4.     {
  5.       super.onActivityResult(requestCode, resultCode, data);
  6.      
  7.       switch(requestCode)
  8.       {
  9.         case(DIRECTORY_SELECTION_DIALOG_CODE):
  10.         {
  11.           if (resultCode == Activity.RESULT_OK)
  12.             sTargetDirectory = data.getStringExtra(DirectorySelectionDialog.TARGET_DIRECTORY);
  13.             Log.i("MAIN ACTIVITY -> onActivityResult", sTargetDirectory);
  14.           break;
  15.         }
  16.       }
  17.      
  18.     }
  19.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4



In the "onCreate" function of the subactivity, i create two buttons (OK, Cancel) an set their OnClickHandler.
Here one sample:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2. public void onCreate(Bundle savedInstanceState)
  3. {
  4. ...
  5. btnOK = (Button)findViewById(R.id.dlg_sel_dir_btn_OK);
  6.  
  7.     btnOK.setOnClickListener(
  8.       new View.OnClickListener()
  9.       {
  10.         public void onClick(View v)
  11.         {
  12.           String targetPath = mCurrentDirectory.toString();
  13.          
  14.           Intent result = new Intent();
  15.           result.putExtra(TARGET_DIRECTORY, targetPath);
  16.          
  17.           setResult(RESULT_OK, result);
  18.           Log.i("DIALOG ACTIVITY -> onActivityResult", "RESULT_OK");
  19.           finish();      
  20.         }
  21.       }
  22.     );
  23. ...
  24. }
  25.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4



Now, please take a closer look at the two "Log-Lines".
In the DDMS-View the line from "onActivityResult" is added before i click the OK-Button from the subactivity.

Why the hell onActivityResult is called before i call setResult in subactivity??? :shock:

I hope i did explain well enough or forgot nothing. Please help.

Greets

BlackDragon

//EDIT: Added solved tag to title...
Last edited by BlackDragon on Sat May 09, 2009 1:08 pm, edited 1 time in total.
BlackDragon
Junior Developer
Junior Developer
 
Posts: 11
Joined: Tue Oct 07, 2008 9:52 am
Location: Germany

Top

Reply

Postby satishkumar_lskin » Sat May 09, 2009 8:18 am

Hi,
It looks as though your sub-activity is destroyed before u can allow for a click event to happen for the buttons. Other than that, Your code seems to be fine with the intention u had in mind.
Can u post the subactivity code to trace where the problem is ?
satishkumar_lskin
Developer
Developer
 
Posts: 32
Joined: Mon Oct 20, 2008 8:15 am
Location: Coimbatore , TamilNadu, India

Postby BlackDragon » Sat May 09, 2009 9:43 am

Hello again,

thanks for your reply. I'll try to give a closer look for the problem and the situation.

If I start the subactivity, the first Log("MAIN ACTIVITY -> onActivityResult") entry is written, then the sub-activity is shown and if I click one of the buttons, the second Log("DIALOG ACTIVITY -> onActivityResult") appears. So I don't think that the sub-activity is destroyed or anything. :?

I'll try to post the relevant code, but i can't post the whole one, because it's a company internal application. :oops: All I can say is, that the sub-activity is a dialog where the user can select a directory.
If it's ready to use, I'll post a workaround, how to implement such a dialog in this forum. :)

What I can assure is, that in this sub-activity no other functions are overriden or any other events are called.

This is the whole onCreate function of the sub-activity:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ...
  2. @Override
  3.   public void onCreate(Bundle savedInstanceState)
  4.   {
  5.     super.onCreate(savedInstanceState);
  6.    
  7.     setContentView(R.layout.dlg_list);
  8.    
  9.     setTitle(mDisplayTitle);
  10.    
  11.     Bundle b = this.getIntent().getExtras();
  12.     String t = b.getString(INITIAL_DIRECTORY);
  13.    
  14.     mCurrentDirectory = new File(t);
  15.    
  16.     lvDirs = (ListView)findViewById(R.id.dlg_sel_dir_directory_list);
  17.    
  18.     tvCurrentDir = (TextView)findViewById(R.id.dlg_sel_dir_current_dir);
  19.    
  20.     lvDirs.setOnItemClickListener(listClickListener);
  21.    
  22.     btnOK = (Button)findViewById(R.id.dlg_sel_dir_btn_OK);
  23.  
  24.     btnOK.setOnClickListener(
  25.       new View.OnClickListener()
  26.       {
  27.         public void onClick(View v)
  28.         {
  29.           String targetPath = mCurrentDirectory.toString();
  30.          
  31.           Intent result = new Intent();
  32.           result.putExtra(TARGET_DIRECTORY, targetPath);
  33.          
  34.           setResult(RESULT_OK, result);
  35.           Log.i("DIALOG ACTIVITY -> onActivityResult", "RESULT_OK");
  36.           finish();      
  37.         }
  38.       }
  39.     );
  40.    
  41.     btnCancel = (Button)findViewById(R.id.dlg_sel_dir_btn_Cancel);
  42.  
  43.     btnCancel.setOnClickListener(
  44.       new View.OnClickListener()
  45.       {
  46.         public void onClick(View v)
  47.         {    
  48.           setResult(RESULT_CANCELED);
  49.          
  50.           finish();      
  51.         }
  52.       }
  53.     );
  54.    
  55.     tvMessage = (TextView)findViewById(R.id.dlg_sel_dir_message);
  56.     tvMessage.setText(mDisplayMessage);
  57.    
  58.     tvCurrentLabel = (TextView)findViewById(R.id.dlg_sel_dir_current_dir_label);
  59.     tvCurrentLabel.setText(mDisplayCurrentLabel);
  60.    
  61.     refreshCurrentDirText();
  62.    
  63.     browseTo(mCurrentDirectory);
  64.   }
  65. ...
  66.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


Anything is fine here and will be initialized "perfectly". :wink:


Here are the two onClickListeners, which I tryed before put them directly to the onCreate function:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ...
  2. private class OkListener implements android.view.View.OnClickListener
  3.   {
  4.     public void onClick(View v)
  5.     {
  6.       String targetPath = mCurrentDirectory.toString();
  7.      
  8.       Intent result = new Intent();
  9.       result.putExtra(TARGET_DIRECTORY, targetPath);
  10.      
  11.       DirectorySelectionDialog.this.setResult(RESULT_OK, result);
  12.      
  13.       finish();      
  14.     }
  15.   }
  16.  
  17.   private class CancelListener implements android.view.View.OnClickListener
  18.   {
  19.     public void onClick(View v)
  20.     {    
  21.       DirectorySelectionDialog.this.setResult(RESULT_CANCELED);
  22.      
  23.       finish();      
  24.     }
  25.   }
  26. ...
  27.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


Now I'll post a list with names of the overridden functions from the main-activity.
    *onCreate
    *onRetainNonConfigurationInstance
    *onActivityResult
    *onCreateOptionsMenu
    *onCreateContextMenu
    *onContextItemSelected

I don't think that any of this functions will disturb the flow.

Maybe the problem is, to call the sub-activity from a context menu??? :?:

click ListItem -> click ContextMenu -> call SubActivity -> write Log entry (from onActivityResult) -> show sub-activity
-> click button -> write Log entry (from onClickListener in subactivity) -> finish sub-activity


?#%&$§ grrrr.... :x

I'm still very confused about this behavior and hope this enough but not to much information. :?


Greets

BlackDragon
BlackDragon
Junior Developer
Junior Developer
 
Posts: 11
Joined: Tue Oct 07, 2008 9:52 am
Location: Germany

Postby padde » Sat May 09, 2009 10:53 am

please debug the resultcode in the onActivityResult method..
i have the same guess as satishkumar_lskin and the resultcode
could approve it.

Greets
Padde
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby BlackDragon » Sat May 09, 2009 11:15 am

Hi.

The resultcode is 0 (Activity.RESULT_CANCELED).

Then, in the sub-activity i click the OK-Button and nothing happens. So onActivityResult in main-activity isn't called. :shock:

If I understand, the meaning of the satishkumar_lskin's post was, that the sub-activity is destroyed, but how it is possible then to show it or click the buttons, if it's destroyed.

Or did i missunderstand something? :?: :?: :?:

Like I wrote in my last post, at the moment I'm a little bit confused about the whole workflow. :?

Greets

BlackDragon

-> EDIT: I forgot to write the version of the SDK I am using. It's the latest version 1.5r1.
BlackDragon
Junior Developer
Junior Developer
 
Posts: 11
Joined: Tue Oct 07, 2008 9:52 am
Location: Germany

Postby Hebus » Sat May 09, 2009 11:21 am

You should try to set your subactivity as single task
Hebus
Junior Developer
Junior Developer
 
Posts: 14
Joined: Wed Apr 15, 2009 1:10 pm

Top

Postby BlackDragon » Sat May 09, 2009 11:33 am

Hi again,

for now I'll thank everybody who tried to find a solution for my problem and I'm still in hope to find one. :)

@Hebus:
I did so in the manifest file.

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. ...
  3.  
  4. <activity android:name=".DirectorySelectionDialog"
  5.  
  6.          android:theme="@android:style/Theme.Dialog"
  7.  
  8.          android:launchMode="singleTask">
  9.  
  10.           <intent-filter>
  11.  
  12.             <action android:name="android.intent.action.VIEW" />
  13.  
  14.             <category android:name="android.intent.category.DEFAULT" />
  15.  
  16.           </intent-filter>
  17.  
  18.         </activity>
  19.  
  20. ...
  21.  
  22.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


Thanks...

BlackDragon
BlackDragon
Junior Developer
Junior Developer
 
Posts: 11
Joined: Tue Oct 07, 2008 9:52 am
Location: Germany

SOLVED

Postby BlackDragon » Sat May 09, 2009 1:06 pm

Hello again,

i got it... :D :D :D it works... :D :D :D

After the broad hint from Hebus, I visit the documentation for launch mode.
I removed the part in the manifest and restarted the app and was happy to see that it works perfectly now. :)

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. ...
  2. <activity android:name=".DirectorySelectionDialog"
  3.          android:theme="@android:style/Theme.Dialog">
  4.           <!-- android:launchMode="singleTask"  --> <!-- This one was the problem !!! -->
  5.           <intent-filter>
  6.             <action android:name="android.intent.action.VIEW" />
  7.             <category android:name="android.intent.category.DEFAULT" />
  8.           </intent-filter>
  9.         </activity>
  10. ...
  11.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


And again biiiig thanks to everybody here...

Greets

BlackDragon
BlackDragon
Junior Developer
Junior Developer
 
Posts: 11
Joined: Tue Oct 07, 2008 9:52 am
Location: Germany

Postby padde » Sat May 09, 2009 1:13 pm

Okay.. so this means the subactivity gets closed somehow..
to double check that make some debug output in the onStop method
of the subactivity.
And comment this super.onActivitResult call out in your onActivityResult method
in the main activity.. its not necessary and could change your expected resultcode
and requestcode.

--- Edit ---
Okay when its fixed you can ignore my guesswork :)


Greets
Padde
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby BlackDragon » Sat May 09, 2009 1:35 pm

Okay padde, and thanks to you for your help.

These steps I was implementing and then I got this solving awareness. :)

Greets

BlackDragon
BlackDragon
Junior Developer
Junior Developer
 
Posts: 11
Joined: Tue Oct 07, 2008 9:52 am
Location: Germany

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests