OnResume OnStop在Android中更改活动的问题

时间:2014-12-02 09:38:16

标签: android android-activity lifecycle

我只是想了解如何改变活动,我做了一个简单的例子但却没有。 问题是,在更改为第二项活动后,我不知道如何回到主要活动。

这是MainActivity.java

public class MainActivity extends ActionBarActivity implements OnClickListener{
private String DEBUG_TAG1 = "tag";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button pauseButton = (Button) findViewById(R.id.pause_button);
      pauseButton.setOnClickListener(this);
}

@Override
   public void onClick(View v)
   {

       if (v.getId() == R.id.pause_button)
       {
           Log.d(DEBUG_TAG1,"pause");
           Intent i = new Intent(this, SecondActivity.class);
           Log.d(DEBUG_TAG1,"intent created");
           i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
           startActivity(i);


       }


   }

protected void onStop() {
    super.onStop();  
    Log.d(DEBUG_TAG1,"on stop");

}
protected void onResume() {
    super.onResume();
    Log.d(DEBUG_TAG1,"on resume");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

这里是SecondActivity.java

public class SecondActivity extends ActionBarActivity implements OnClickListener{
private String DEBUG_TAG2 = "tag";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    Button pauseButton2 = (Button) findViewById(R.id.pause_button2);
    pauseButton2.setOnClickListener(this); 

   }  
@Override
   public void onClick(View v)
   {

       if(v.getId() == R.id.pause_button2)
       {
           //here i have to get back to the MainActivity

       }

   }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

以下是XML文件:

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="net.iversoncru.changeact.MainActivity" >

<Button
    android:id="@+id/pause_button"

    android:layout_gravity="center"

    android:text="||"
    android:textColor="#ff0000"
    android:textSize="18sp"
    android:textStyle="bold"

    android:layout_width="70dp"
    android:layout_height="70dp"
    android:background="@drawable/buttonshape"
    android:shadowColor="#A8A8A8"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="5"
    />

</LinearLayout>

和activity_second.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#000000"
tools:context="net.iversoncru.changeact.SecondActivity" >

<Button
    android:id="@+id/pause_button2"

    android:layout_gravity="center"

    android:text="||"
    android:textColor="#aa0000"
    android:textSize="18sp"
    android:textStyle="bold"

    android:layout_width="90dp"
    android:layout_height="90dp"
    android:background="@drawable/buttonshape"
    android:shadowColor="#A8A8A8"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="5"
    />

</LinearLayout>

谁能告诉我怎么样?

我注意到,当SecondActivity启动时,在MainActivity上调用OnStop()。 我如何使用OnResume()?

另外我读到也许我必须保存MainActivity的状态才能回到它...

我有点失落。

1 个答案:

答案 0 :(得分:0)

@Override
   public void onClick(View v)
   {

       if(v.getId() == R.id.pause_button2)
       {
           //here i have to get back to the MainActivity
           finish();
       }

   }

当您切换到下一个活动时,OS会在堆栈上推送当前活动并提前请求的活动。

来自活动类的

finish()方法将向系统宣布您已完成当前活动中的工作并恢复我之前在堆栈上的活动。