开发者

Passing values with intents

I have searched and searched and I just can't get this code to work.I have a main.xml layout and a setting.xml.I have some values I would like the Se开发者_如何学Cttings.class to change in my main apps class.Three string to be exact.

I have tried this simple test code in my main app class

settings.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    Intent intent = new Intent(v.getContext(), Settings.class);
     startActivityForResult(intent, 0);


   }
   });

//Then a function
 @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent intent){
      super.onActivityResult(requestCode, resultCode, intent);
      Bundle extras = intent.getExtras();
      String value = extras.getString("myKey");
      if(value!=null){
      Log.d("hmmm",value);
      }
  }
}

In my settings.class I have the following

 returnHome.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {
     Intent intent = new Intent();
     intent.putExtra("myKey", "YEAH"); 
                 setResult(RESULT_OK, intent);
                 finish();


    }

         });

Back in main app class it is not getting logged.

Like I said I have three string in the main class that I want settings class to change and send back. Any help is greatly appreciated


I have had success with the technique used in the Notepad tutorial where the information is placed in a Bundle and then added to the intent. See Step 10:

Bundle bundle = new Bundle();

bundle.putString(NotesDbAdapter.KEY_TITLE, mTitleText.getText().toString());
bundle.putString(NotesDbAdapter.KEY_BODY, mBodyText.getText().toString());
if (mRowId != null) {
    bundle.putLong(NotesDbAdapter.KEY_ROWID, mRowId);
}

Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
finish();


Your code works fine for me, here's the whole lot

Main.java

package com.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Main extends Activity {
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      Button settings = (Button) findViewById(R.id.settings);
      settings.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent intent = new Intent(v.getContext(), Settings.class);
            startActivityForResult(intent, 0);
         }
      });
   }
   @Override
   protected void onActivityResult(int requestCode, int resultCode,
         Intent intent) {
      super.onActivityResult(requestCode, resultCode, intent);
      Bundle extras = intent.getExtras();
      String value = extras.getString("myKey");
      if (value != null) {
         Log.d("hmmm", value);
      }
   }
}

Settings.java

package com.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Settings extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.settings);
      Button returnHome = (Button) findViewById(R.id.returnHome);
      returnHome.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent intent = new Intent();
            intent.putExtra("myKey", "YEAH");
            setResult(RESULT_OK, intent);
            finish();
         }
      });
   }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
      <Button
            android:text="Settings"
            android:id="@+id/settings"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"></Button>
</LinearLayout>

settings.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
      <Button
            android:text="Return home"
            android:id="@+id/returnHome"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"></Button>
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest
      xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.test"
      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="Settings"
                  android:label="Settings" />
      </application>
</manifest>

And the logcat came out:

11-17 12:21:46.717: DEBUG/hmmm(258): YEAH
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜