How to make a second transition through a fragment - java

I made the first transition from the fragment to the activity, everything works fine, but I can't make the second transition. I'm a beginner, and I understand that this is a stupid mistake.
View v = inflater.inflate(R.layout.activity_physics, null);
Button phys35 = (Button) v.findViewById(R.id.phys35);
phys35.setOnClickListener(new View.OnClickListener() {
Public void onClick(View v) {
Intent intent = new Intent(getContext(), Phys35.class);
StartActivity(intent);
}
});
return v;
Button phys36 = (Button) v.findViewById(R.id.phys36);
phys36.setOnClickListener(new View.OnClickListener() {
Public void onClick(View v) {
Intent intent = new Intent(getContext(), Phys36.class);
StartActivity(intent);
}
});
return v;
Please help, thank you in advance for your help.

Related

Problems with intent in second activity

I am new in the android dev and java
My idea is to make BMI that will have history.
So I am at the point where I struggle with the intent for the second activity.
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openActivity2();
}
});
}
private void openActivity2() {
Intent intent = new intent(this, Activity2.class);
startActivity(intent);
}
I am getting:
can not resolve symbol intent
'i' should capital when you create new Intent() object The openActivity2() should look like this
private void openActivity2() {
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
}

How to stop looping in activites and at the same handle the default on screen navigation back?

ImageView Back = findViewById(R.id.imageView11);
Back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Wallet.this, Nav.class);
startActivity(intent);
finish();
}
});
Button cash = findViewById(R.id.button11);
cash.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Wallet.this, Cash.class);
startActivity(intent);
finish();
}
});
TextView Card = findViewById(R.id.textView26);
Card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Wallet.this, Card.class);
startActivity(intent);
finish();
}
});
Button AddCard = findViewById(R.id.button12);
AddCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Wallet.this, AddedCard.class);
startActivity(intent);
finish();
}
});
TextView promo = findViewById(R.id.textView28);
promo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Wallet.this, promocode.class);
startActivity(intent);
finish();
}
});
Button Voucher = findViewById(R.id.button13);
Voucher.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Wallet.this, Vouchers.class);
startActivity(intent);
finish();
}
});
}
}
If i remove finish(); this create loops when I press default navigation back button.
like if I visited promocode class,then voucher class.from vouchar class I want to go back in Main Class by pressing default navigation back button.it will reverse like vouchar to promode then main class.
But I want by default back button will bring me vouchar class to directly main class.
But after adding finish(); whenever i press navigation back button the app quits.
I want a solution that...default navigation back button will act like my every customized back button for every each individuals frames !!
The key is to provide proper navigation.
Here's the Official Documentation to understand the tasks and back stack.
The other way to provide back navigation to the custom activity is to override onBackPressed() method of your activity like :
#Override
public void onBackPressed()
{
super.onBackPressed();
startActivity(new Intent(YourCurrentActivity.this, MainActivity.class));
finish();
}

Android - how to switch activity on image click

Here I've got this code
switchact.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent act2 = new Intent(view.getContext(), Activity2.class);
startActivity(act2);
}
});
And i've got image on my activity/layout1.
How do I make that when I click on the image it switched to activity 2?
You should set a listener on the image over the onClick event, try something like:
ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent act2 = new Intent(view.getContext(), Activity2.class);
startActivity(act2);
}
});
Intent act2 = new Intent(Activity1.this, Activity2.class);
startActivity(act2);
ImageView img = (ImageView) findViewById(R.id.myImageId);
img.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent act2 = new Intent(Activity1.this, Activity2.class);
startActivity(act2);
}
});

Android OnClickListener, intent and context

I am new to java and android. Here I am trying to set up my onclicklistener so when clicked, it will show another activity, i.e. ActivityB.class. The problem is with Intent i = new Intent(context, ActivityB.class); I am not sure what to put there for context. I tried to use this and context, and both are wrong.
Could you please kindly explain when and why I should use this and when to use other terms for the context?
public class MainActivity extends Activity {
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(context, ActivityB.class);
startActivity(i);
}
});
}
Change the code to.
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, ActivityB.class);
startActivity(i);
}
});
As you need to pass context while using intent.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, ActivityB.class));
}
});
Fastest method!
Hope it helps! :D
1) Replace context with getApplicationContext()
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),ActivityB.class);
startActivity(intent);
}
});
2) Replace context with MainActivity.this
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,ActivityB.class);
startActivity(intent);
}
});
Hope it will help you!
Try this..
If you declare ActivityB.class in manifest this should work.
Intent i = new Intent(MainActivity.this, ActivityB.class);
startActivity(i);
Intent i = new Intent(this, ActivityB.class);
startActivity(i);

Remove activities manually from Android app stack

I been working on Android Native App , What i was trying to do is :
Activities - A -> B -> C Then A-> B -> C -> C .
From C Activity if it again point to C then i want to remove C , B from stack manually .
On my back it should move only to A .
I tried finish() but problem is :
Activities - A -> B -> C Then A-> B -> C -> C on finish A -> B -> C required state A-> C .
Is anyone know how to catch all activities in stack and remove specific activities from stack ??
In Activity C, override onBackPressed and add in something like:
#Override
public void onBackPressed() {
if (shouldGoBackToA) { // There are various ways this could be set
Intent intent = new Intent(this, AActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else {
finish();
}
}
FLAG_ACTIVITY_CLEAR_TOP will cause it to go down the stack to the existing copy of A Activity instead of starting a new one. From the docs:
public static final int FLAG_ACTIVITY_CLEAR_TOP
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
While calling intent pass a flag called actvity clear top like this:
Intent newIntent=new Intent(this,MainActivity.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(newIntent);
You can use this :
In A activity while passing to B activity, the intent should be added with a flag FLAG_ACTIVITY_NO_HISTORY like this,
Button b=(Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent newIntent=new Intent(AActivity.this,Bactivty.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(newIntent);
}
});
While moving to CActivity:
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent newIntent = new Intent(Bactivty.this, CActivity.class);
startActivity(newIntent);
}
});
On backpress will take you to AActivity now.
Step 1: Start activty for result A -> B -> C1 -> C2..
Call your Activity with startActivityForResult
Intent intent = new Intent(yourActivity.this, nextActivity.class);
startActivityForResult(intent, 1);
Step 2: In C2 specify that you want to go back to A..
Whenever you are done with your activity write the below code
Intent i = getIntent();
i.putString("Result","GottoA");
setResult(Activity.RESULT_OK, i);
finish();
Step 3: Whenever C2 finishes , previsus stack activit's onActivityResult is called.. so u can check in C1 and B onActivityResult whether you have set any result bck.. and finish accordingly
and impliment the following code in Activity B and c
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Intent i = getIntent();
if (resultCode == RESULT_OK && i.getString("Result","null").equals"GottoA") {
i.putString("Result","GottoA");
setResult(RESULT_OK, i);
finish();
}
}
In Activity C, when back button is pushed start activity A like this:
#Override
public void onBackPressed() {
Intent intent = new Intent(getApplicationContext(), A.class);
intent.putExtra("EXIT", true);
startActivity(intent);
}
Then in Activity A's onCreate() do this
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
this complete example may help you...
public class ActivityA extends Activity {
public static final int ID_TEXTVIEW = 0xDEAF1;
public static final int ID_BUTTON = 0xDEAF2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View contentView = getContentView(this);
TextView textView = (TextView) contentView.findViewById(ID_TEXTVIEW);
textView.setText("ActivityA");
setContentView(contentView);
final Button button = (Button) contentView.findViewById(ID_BUTTON);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
}
});
}
public static View getContentView(Context context) {
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
TextView textView = new TextView(context);
textView.setLayoutParams(layoutParams);
textView.setId(ID_TEXTVIEW);
layout.addView(textView);
Button button = new Button(context);
button.setText("Next");
button.setLayoutParams(layoutParams);
button.setId(ID_BUTTON);
layout.addView(button);
return layout;
}
}
public class ActivityB extends Activity {
public static final String ACTION_FINISH = "com.myapp.test2.ACTION_FINISH";
public ActivityB() {
}
private FinishReceiver finishReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View contentView = ActivityA.getContentView(this);
final TextView textView = (TextView) contentView
.findViewById(ActivityA.ID_TEXTVIEW);
textView.setText("ActivityB");
setContentView(contentView);
final Button button = (Button) contentView
.findViewById(ActivityA.ID_BUTTON);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ActivityB.this, ActivityC.class);
startActivity(intent);
}
});
finishReceiver = new FinishReceiver();
IntentFilter filter = new IntentFilter(ACTION_FINISH);
registerReceiver(finishReceiver, filter);
}
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(finishReceiver);
}
private class FinishReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_FINISH)) {
finish();
}
}
}
}
public class ActivityC extends Activity {
public ActivityC() {
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View contentView = ActivityA.getContentView(this);
final TextView textView = (TextView) contentView
.findViewById(ActivityA.ID_TEXTVIEW);
textView.setText("ActivityC");
setContentView(contentView);
final Button button = (Button) contentView.findViewById(ActivityA.ID_BUTTON);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ActivityB.ACTION_FINISH);
sendBroadcast(intent);
intent = new Intent(ActivityC.this, ActivityC.class);
startActivity(intent);
finish();
}
});
}
}

Categories

Resources