将意图传递给Android中的上一个活动?

时间:2016-02-23 10:38:31

标签: android

按钮导航到上一个活动,但为什么不将意图传递给上一个活动。

这是我的代码。

 btnSaveRecord.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                /*
                finish();*/

               /* Intent i = new Intent(Record_Audio.this, AddPost.class);
                i.putExtra("STRING_I_NEED", newAudioFile);
                //setResult(8, i);
                //finish();//finishing activity
                startActivityForResult(i,8);
*/
                getIntent().putExtra("STRING_I_NEED", newAudioFile);
                setResult(RESULT_OK, getIntent());
                finish();
                overridePendingTransition(R.anim.back, R.anim.back_out);
            }
        });

这是onActivityResult

中的先前活动代码
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
{
 if(requestCode==1010 && resultCode==RESULT_OK)
                {
                    String STRING_I_NEED=data.getStringExtra("STRING_I_NEED");
                    Log.e("","STRING_I_NEED = "+STRING_I_NEED);}

}
}

4 个答案:

答案 0 :(得分:2)

btnSaveRecord.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent i = new Intent();
            i.putExtra("STRING_I_NEED", newAudioFile);
            setResult(RESULT_OK, i);
            finish();
            overridePendingTransition(R.anim.back, R.anim.back_out);
        }
    });

答案 1 :(得分:1)

您需要使用新的Intent将数据传回上一个活动。

更改

getIntent().putExtra("STRING_I_NEED", newAudioFile);
setResult(RESULT_OK, getIntent());

Intent intent=new Intent();  
intent.putExtra("STRING_I_NEED", newAudioFile);
setResult(RESULT_OK,intent);  

答案 2 :(得分:0)

<?php
// Configuration
if (is_file('config.php')) {
   require_once('config.php');
}
// Startup
require_once(DIR_SYSTEM . 'startup.php');

// Registry
$registry = new Registry();

// Config
$config = new Config();
$registry->set('config', $config);


$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$registry->set('db', $db);

class myClass
{
   protected $db;

   public function __construct($db)
   {
      $this->db = $db;
   }
   // Database
   function repairCategories($parent_id = 0)
   {
      $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "category` WHERE `parent_id` = '" . 

(int)$parent_id . "'");
      echo "SELECT * FROM `" . DB_PREFIX . "category` WHERE `parent_id` = '" . (int)$parent_id . 

"'<br><br>";

      foreach ($query->rows as $row)
      {
         // Delete the path below the current one
         echo "DELETE FROM `" . DB_PREFIX . "category_path` WHERE `category_id` = '" . (int)$row

['category_id'] . "'<br>";
         $this->db->query("DELETE FROM `" . DB_PREFIX . "category_path` WHERE `category_id` = '" . 

(int)$row['category_id'] . "'");

         // Fix for records with no paths
         $level = 0;

         $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "category_path` WHERE 

`category_id` = '" . (int)$parent_id . "' ORDER BY `level` ASC");
         echo "SELECT * FROM `" . DB_PREFIX . "category_path` WHERE `category_id` = '" . 

(int)$parent_id . "' ORDER BY `level` ASC<br>";

答案 3 :(得分:0)

您创建了两个活动

第二个活动

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    Intent i=new Intent();
    i.putExtra("msg", text);
    setResult(RESULT_OK,i);
    finish();
    Log.i("","<<<<<<<<<<<<<<<<<<<<<<"+text );
    super.onBackPressed();
}

on First Actvivty

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

            String str = data.getStringExtra("msg");

            tv.setText(str);
             tv1=tv.getText().toString();
             tv2 = tv1;

            Log.d(">>>>>>>>>>>>>>>>>>", "" + tv2);

    super.onActivityResult(requestCode, resultCode, data);

}