开发者

[Android]How to get results from intent launched from preference screen?

开发者 https://www.devze.com 2023-01-10 00:30 出处:网络
I need help in geting results back from intent launched from preference screen // Intent preference 开发者_开发技巧

I need help in geting results back from intent launched from preference screen

   // Intent preference 开发者_开发技巧
   DevicePref = 
   getPreferenceManager().createPreferenceScreen(this); 

   // Show a Screen with list of Devices Discovered 
   Intent i = new Intent(this,getDevice.class); 
   DevicePref.setIntent(i); 
   DevicePref.setTitle("Select Device"); 
   DevicePref.setSummary(mSelectedDevice); 
   deviceOptionsCat.addPreference(DevicePref); 

I want user to select device... In preference screeen I show "Select Device" .. when user clicks that, another screen is launched by intent where all devices are listed. User selects the device.

Now how do I know user selected which device? And I want to update that in the summary.

Pls. let me know Thanks


I got the answer, Hope it will help someone like me...

Do not mention intent while creating preference like I did in above code.. Mention intent on OnPreferenceClickListener and then do StartActivityForResult()

    // Intent preference 
   DevicePref = getPreferenceManager().createPreferenceScreen(this); 
   // Show a Screen with list of Devices Discovered 

   DevicePref.setOnPreferenceClickListener(onPreferenceClick);

   DevicePref.setTitle("Select Device"); 
   DevicePref.setSummary(mSelectedDevice); 
   deviceOptionsCat.addPreference(DevicePref); 

Then create OnPreferenceClickListner and here do StartActivityFromResult()

OnPreferenceClickListener onPreferenceClick = new Preference.OnPreferenceClickListener() {
       public boolean onPreferenceClick(Preference preference) {

           if (preference ==DevicePref )
           {
               Intent i = new Intent(DevuiceOptions.this,getDevice.class);  
               DevicePref.setIntent(i); 
               startActivityForResult(i,CHOOSE_DEVICE);

           }
           return true;
       }
   };

Finally to get the result handle onActivityResult and update Summary field.

@Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data)
   {

       switch (requestCode) {

           case Constants.CHOOSE_DEVICE:
           {
               if (data!=null )
               {
                   Bundle b = data.getExtras();
                   mSelectedDevice =    (String) b.get("Name");
                   UpdatePreferences();
               }

           }
    }
}

Thanks

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号