简体   繁体   English

完成第二个活动(由第一个活动调用)后如何在第一个活动中调用onCreate方法

[英]How to call onCreate method in 1st activity after finishing 2nd activity(which is called by the 1st activity)

I am a begginer in android. 我是android的初学者。 I have a simple code, where I am calling one activity from my 1st activity and in that 2nd activity I have a button on press of which 2nd activity is finished and 1st activity comes up. 我有一个简单的代码,在这里我从第一个活动中调用一个活动,在第二个活动中,我有一个按钮可以按下,第二个活动已完成,第一个活动出现了。 Is there any way to call the onCreate method in the 1st activity as the onCreate method is never called in the 1st activity(onResume is called always)? 有什么方法可以在第一个活动中调用onCreate方法,因为从未在第一个活动中调用过onCreate方法(总是调用onResume)? Do i have edit some thing in the manifest file. 我是否在清单文件中编辑了某些内容。

Following is my Code 以下是我的代码

 public class Activity1 extends Activity {
  /** Called when the activity is first created. */

 TextView mTextView ;

 Button b1;
 static int count=0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    mTextView = (TextView) findViewById(R.id.textView2);

    if (savedInstanceState == null) {
        mTextView.setText("Welcome to HelloAndroid!");
    } else {
        mTextView.setText("Welcome Back!");
        System.out.println("count------>"+ count);
    }



    final Intent i = new Intent(this,activity2.class);
    b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            startActivity(i);

        }
    });

}


@Override
public void onResume() 
{
    super.onResume();
    System.out.println("inside Resume");
 }

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
      super.onSaveInstanceState(savedInstanceState);

      savedInstanceState.putBoolean("MyBoolean", true);
      savedInstanceState.putDouble("myDouble", 1.9);
      savedInstanceState.putInt("MyInt", 1);
      savedInstanceState.putString("MyString", "Welcome back to Android");
      count++;
    }


    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);

      boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
      double myDouble = savedInstanceState.getDouble("myDouble");

      System.out.println("MyBoolean"+ myBoolean);
      System.out.println("myDouble"+ myDouble);
    }


}

and here is my 2nd activity which is called by the 1st activiy 这是我的第二项活动,第一项活动称为

public class activity2 extends Activity{

TextView textview;
Button b1;
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);

        textview = (TextView) findViewById(R.id.textView1);

        textview.setText("in activity2");

        b1 = (Button) findViewById(R.id.button1);

        b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                finish();
            }
        });
 }
}

Some one please help me Thanks! 有人请帮助我,谢谢!

In first Activity,call finish() after startActivity(i); 在第一个活动中,在startActivity(i);之后调用finish() startActivity(i); and in second Activity,start first Activity before calling finish() .When you start an Activity that has not instance,it's onCreate will be called. 在第二个Activity中,在调用finish()之前启动第一个Activity。当您启动一个没有实例的Activity时,它将调用onCreate

Edit: 编辑:

If you want to save state of first Activity,you can create a bundle and add state of your views to it.Then add this bundle as extra to intent that starts second Activity.In second Activity get this extra from intent and when you want to start first Activity via intent(for example startFirstActivity intent) add that bundle to this intent(startFirstActivity intent).Now in onCreate method of first Activity,get bundle from intent(via getIntent().getextras() ) and if it was not null,extract state of your views from it and set your views states after find them by ID. 如果要保存第一个Activity的状态,则可以创建一个包并向其中添加视图的状态。然后将该包作为额外的内容添加到启动第二个Activity的intent中。通过intent启动第一个Activity(例如startFirstActivity intent)将该包添加到该intent(startFirstActivity intent)中。现在在第一个Activity的onCreate方法中,从intent中获取包(通过getIntent().getextras() ),如果不为null ,从中提取视图状态,并在通过ID找到它们后设置视图状态。

Just use finish(); 只需使用finish(); before startActivity(i) in Activity 1 在活动1中的startActivity(i)之前

use this code in second activity2 button click, before moving to Activity2 call finish() in Activity1 after statActivity(); 在第二个activity2按钮单击中使用此代码,然后移到Activity2,然后在statActivity();之后在Activity1调用finish() statActivity();

b1.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View v) {

      Intent i = new Intent(Activity2.this ,Activity1.class);
      startActivity(i);
   }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在第一个活动中调用第二个活动值? - how to call 2nd activity value in 1st activity? 当我返回到第一个活动时,第二个活动未调用第二个活动的onCreate()函数 - When I return back to 1st Activity the onCreate() function of 2nd Activity is not called in 2nd time 如何从第二活动到第一活动获取数据 - How to get data from 2nd Activity to 1st Activity 在第二个活动通过ContentProvider插入数据库后,第一个活动的列表视图未更新; onLoadFinished不被调用 - 1st Activity's listview not updated after 2nd activity inserts into database via ContentProvider; onLoadFinished not called 从第一个活动拨打电话后,第二个活动没有通话 - 2nd Activity doesn't call after calling from 1st Activity 从第一个活动中获取意图后如何刷新第二个活动 - How to refresh 2nd activity after intent from 1st activity 将第1个活动的值传递给android中的第2个活动 - Pass the value from 1st activity to 2nd activity in android 从第一个活动转到第二个活动时出错 - error when going from 1st activity to 2nd activity 第一项活动第二项活动获得纬度经度地理位置。 如何将信息转移回第一活动? - 1st activity 2nd activity to get the latitude longitude geo-location. How to transfer info back to 1st activity? 尝试从第一类活动中调用方法,并在第二类活动中保留相同的参数(android studio) - Trying to call a method from 1st class activity and keep the same argument in a 2nd class activity (android studio)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM