【问题标题】:Using intents for passing data使用意图传递数据
【发布时间】:2011-08-10 06:57:05
【问题描述】:

如何使用意图将数据从一个活动发送到另一个活动。

(注意这是一个两部分的问题,发送和接收)。

我正在创建一个表单,我想将每个问题的答案保存在一个 sqlite 数据库中。

【问题讨论】:

    标签: android forms sqlite android-intent


    【解决方案1】:

    发送(活动 1):

    Intent intent = new Intent(MyCurentActivity.this, SecondActivity.class);
    intent.putExtra("key", "enter value here");
    startActivity(intent); 
    

    接收(活动 2):

    String text = getIntent().getStringExtra("key");
    

    【讨论】:

      【解决方案2】:

      您可以使用 Bundle 轻松传递数据。

      Bundle b=new Bundle();
      b.putString("key", value);
      
      Intent intent=new Intent(TopicListController.this,UnitConverter.class);
      intent.putExtras(b);
      
      startActivity(intent);
      

      您可以在其他活动中接收数据,如下所示:

      Bundle b=this.getIntent().getExtras();
      String s=b.getString("select");
      

      【讨论】:

        【解决方案3】:

        从Activity发送

        Intent myIntent = new Intent(First.this, Second.class);
            myIntent.putExtra("name","My name is");
            startActivity(myIntent);
        

        在秒内检索

        Bundle bundle = getIntent().getExtras();
                   String name=bundle.getString("name");
        

        【讨论】:

          【解决方案4】:
          Intent i = new Intent(yourActivity.this, nextActvity.class);
          
          i.putExtra("dataName", data);
          
          //recieving
          
          Intent i = nextActivity.this.getIntent();
          
          
           string s =  i.getStringExtra("dataName", defaultValue);
          

          【讨论】:

            【解决方案5】:

            要使用当前活动的意图传递数据,请使用以下代码

                         Intent in = new Intent(YourClassContext, nextActivity.class);
                         in.putExtra("name","value");
                         startActivity(in);
            

            要获取在下一个活动中传递的意图数据,请使用以下代码

                 Intent i = nextActivity.this.getIntent();
                 String intentDatapassed = i.getStringExtra("name", defaultValue);
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2016-03-13
              • 2015-04-16
              • 2012-02-26
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多