AliJoooon
AliJoooon

Reputation: 41

Android , start The same activity by intent

Is it possible to start the same activity that is on top by itselt BUT with EXTRA ?

I created ONE activity and 2 Layout, loading each one depends on the EXTRA that is coming. the problem is that the activity must calls itself by like below :

startActivity(new Intent(Ali.this , Ali.class).putEXTRA("which" , "2"));
this.finish();

Upvotes: 2

Views: 1671

Answers (2)

Sukhbir
Sukhbir

Reputation: 1608

Yes this is possible try below code

Button buttonOne = (Button) findViewById(R.id.button1);
buttonOne.setOnClickListener(new Button.OnClickListener() {
 public void onClick(View v) {
   Intent intent = new Intent(MainActivity.this,
                    MainActivity.class);
           intent.putExtra("data","SomeData");
            startActivity(intent);
 }
  });

In Activity onCreate add some check like below code

 @Override
protected void onCreate(Bundle bd) {
    super.onCreate(bd);
    setContentView(R.layout.filter_activity);

    bd = getIntent().getExtras();
    if (bd != null) {
        String data = bd.getString("data");
    }
}

Hope this will help

Upvotes: 1

Flitzpiepe
Flitzpiepe

Reputation: 167

What is hindering you to use Fragments for this? You can replace them considering the context you need.

Upvotes: 0

Related Questions