简体   繁体   中英

How can I resume Activity B from Activity C?

I have 3 classes A,B,C.

Class A am passing 3 values to class B and class B am passing 3 values to class C. In class CI have one back button if I click this back button I want to resume my activity B.

I am new for android; I don't know much about activity life cycle. Thanks in advance.

This is my code.

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);
            }
        });
    }
}

This is the class B Activity

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);
            }
        });
    }
}

This is class C Activity ( where the app crashes )

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);
            }
        });
    }
}

Whenever I click this back button the app crashes, not resuming my Activity B.

To close the C activity and resume the last one, use finish();

So for example, put finish(); in your back button listener

Replace your back button listener with this in your Activity C class:

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

It will close your current Activity (C), and therefore resume your Activity that was active before it (B).

Use like this

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM