0

我有3个班级A,B,C。

A 类将 3 个值传递给 B 类,B 类将 3 个值传递给 C 类。在 CI 类中,如果我单击此返回按钮,则有一个返回按钮,我想恢复我的活动 B。

我是安卓新手;我对活动生命周期了解不多。提前致谢。

这是我的代码。

public class A extends Activity
{
    EditText one,two,three;Button sub;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.one);
        this.onWindowFocusChanged(true);
        one=(EditText)findviewById(R.id.one);
        two=(EditText)findviewById(R.id.one);
        three=(EditText)findviewById(R.id.one);
        addListenerOnButton();
    }
    public void addListenerOnButton() 
    {
        // TODO Auto-generated method stub
        sub.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                Intent i = new Intent(A.this, B.class);
                Bundle b=new Bundle();
                b.putString("one",one.getText().toString());
                b.putString("two",two.getText().toString());
                b.putString("three",three.getText().toString());
                b.putExtras(b);
                startActivity(i);
            }
        });
    }
}

这是 B 类活动

public class B extends Activity
{
    EditText one,two,three;Button sub;String one1,two1,three1;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.two);
        this.onWindowFocusChanged(true);
        one=(EditText)findviewById(R.id.one);
        two=(EditText)findviewById(R.id.one);
        three=(EditText)findviewById(R.id.one);
        Bundle b = this.getIntent().getExtras();
        one1=b.getString("one");
        one.setText(one1);
        two1=b.getString("two");
        two.setText(two1);
        three1=b.getString("three");
        three.setText(three1);
        addListenerOnButton();
    }
    public void addListenerOnButton() 
    {
        // TODO Auto-generated method stub
        sub.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                Intent i = new Intent(B.this, C.class);
                Bundle b=new Bundle();
                b.putString("one",one.getText().toString());
                b.putString("two",two.getText().toString());
                b.putString("three",three.getText().toString());
                b.putExtras(b);
                startActivity(i);
            }
        });
    }
}

这是 C 类活动(应用程序崩溃的地方

public class C extends Activity
{
    EditText one,two,three;Button sub,back;String one1,two1,three1;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.three);
        this.onWindowFocusChanged(true);
        one=(EditText)findviewById(R.id.one);
        two=(EditText)findviewById(R.id.one);
        three=(EditText)findviewById(R.id.one);
        Bundle b = this.getIntent().getExtras();
        one1=b.getString("one");
        one.setText(one1);
        two1=b.getString("two");
        two.setText(two1);
        three1=b.getString("three");
        three.setText(three1);
        addListenerOnButton();
    }
    public void addListenerOnButton() 
    {
        back.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                //Clicking back button i want to resume the activity b class but its getting crashed
                Intent i = new Intent(C.this, B.class);
                startActivity(i);
            }
        });
    }
}

每当我单击此后退按钮时,应用程序就会崩溃,而不是恢复我的 Activity B。

4

3 回答 3

2

要关闭 C 活动并恢复最后一个活动,请使用finish();

因此,例如,放入finish();您的后退按钮侦听器

于 2013-11-13T14:34:19.777 回答
0

在 Activity C 类中用这个替换你的后退按钮监听器:

back.setOnClickListener(new OnClickListener() 
{
    @Override
    public void onClick(View v) 
    {
        finish();
    }
}

它将关闭您当前的 Activity (C),因此恢复之前处于活动状态的 Activity (B)。

于 2013-10-16T05:44:00.107 回答
0

像这样使用

public class Main1Activity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        ((TextView)findViewById(R.id.txt)).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                onBackPressed();
            }
        });
    }
    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
    }
}
于 2013-10-16T06:01:34.970 回答