【问题标题】:Launching activity through intents通过意图启动活动
【发布时间】:2011-04-09 22:26:19
【问题描述】:

我正在实现一个简单的应用程序。我需要根据 Activity 的状态启动一个 Activity。让我用一个按钮开始活动。 1.如果activity没有启动,我需要启动XYZ activity。 2. 如果 XYZ 活动处于焦点位置,那么我需要在按下按钮时关闭活动。 3.如果XYZ活动没有处于焦点(如onPause)状态,那么我需要改变按钮状态。

你能帮我解决我需要用来启动意图的标志吗? 是否可以在我开始该活动之前获取该活动的状态?

【问题讨论】:

    标签: android android-activity android-intent


    【解决方案1】:

    试试这个

    Intent 意图 = new Intent(currentActivity.this, callingActivity.class); 开始活动(意图);

    您可以使用这样的意图来调用活动

    【讨论】:

      【解决方案2】:

      每个活动都需要单独捕获按钮按下,因此只需将不同的响应编码到每个不同的活动中。

      【讨论】:

        【解决方案3】:

        首先创建一个MAIN.java 活动来容纳您的其他活动。就像其他人所说的那样,您将不得不对捕获自己的按钮进行编码,因为如果您要尝试处理意图,那应该是常识。但是,当您将它们组合在一起时,您可以通过以下意图开始一个新活动:

        // allocate new intent, initialized to the activity you wish to launch
        Intent i = new Intent(this, ActivityToBeLaunched.class);
        
        // put information into intent
        i.putExtras("KeyName", value); // where "KeyName" is simply a reference string
                                 // and 'value' can be anything from boolean - string.
        
        // launch activity and wait for response
        startActivityForResult(i, REQUEST_CODE);
        

        然后在您的 ActivityToBeLaunched.java 类中,您将拥有一个 oncreate,它将从 Intent 中提取信息,如下所示:

        // get intent
        Intent i = this.getIntent();
        
        // get information from intent
        booleanVariable = i.getExtras().getBoolean("KeyName");
        

        完成此活动后,只需使用;

           // create intent
           Intent i = new Intent();
        
           // put information into result to send back to parent
           i.putExtras("KeyName", value);
        
           // set the result to be returned
           setResult(i, ResultCode);
        
            // finish child, return to parent with results
            finish();
        

        【讨论】:

          猜你喜欢
          • 2016-10-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-11
          • 2011-08-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多