how to disable the previous activity when click on the next activity - android

I am having multiple activity in my program the flow of it is like MainActivity-LoginActivity-NextLoginActivity etc.,I am applying transparency theme on each activity except main activity so I want when I click any activity it should show MainActivity in background rather than previous activity.

Maybe you want result below?
You open app, app into these activity in order: MainActivity -> LoginActivity -> OtherActivity
And you want to click a button that in OtherActivity, you don't want to return the LoginActivity but MainActivity
If it is, you can do this:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
In this line: intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Activity will use the Launch Mode by singleTask.
If activity stack has MainActivity, it will finish other activity that on top of MainActivity, then return MainActivity.

You can try to give an id to the parent layout in each activity and handle clicks on it.
Something like:
LinearLayout LoginActivity = (LinearLayout) findViewById(R.id.login_activity);
LoginActivity.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(this, MainActivity));
//If you want to close this one
//this.finish();
}
});

Related

Fragments - How to

I have three fragments in two activities;fragmentA in activityA and fragmentB,fragmentC in activityB.when i click a button in fragmentA i want to go specifically to fragmentB (not fragment c) in activityB. please help me in this regard.
add this in OnCreateView of Fragment A:
Button button = v.findViewById(R.id.mybutton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ActivityB.class);
startActivity(intent);
}
});

Android layout doesn't "refresh" until I start a new activity

I have this problem. I wrote a program and I have this code in the MainActivity class:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent activity = new Intent(MainActivity.this, AnotherActivity.class);
startActivity(activity);
output.setText(object.toString());
}
});
In AnotherActivity I modify the object and after that activity ends (with finish()) i want to "refresh" the textView of the MainActivity.
At the end of AnotherActivity the object is actually modified, but the text is not refreshed. If I click again on button, before new Activity is started (with his layout) the text is refreshed as it should, and if I close the AnotherActivity layout, the text is well refreshed. But if I don't click again that button, the text remains the old one. How can I do? Sorry for bad English or bad explanation.
You can use onResume() in the first activity to update your UI when the second activity is finished.
Either that or start the second activity with startActivityForResult which will call onActivityResult for you when the second activity is finished.

How can i load another activity under tab - android

i created one tab menu.when i click the tab it opens one activity under the tab that activity contains two buttons.when clicking the buttons it open the new activity it does not open a activity under that tab itself.how should i open a another activity under the same menu.please help friends.
i used this code to open a another activity when button was clicked
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(SongsActivity.this,FiveActivity.class);
startActivity(intent);
finish();
} });
button2.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent obj=new Intent("com.layout.Contactregistration");
startActivity(obj);
}
});
If i understood your question then You can use simple logic for your button click.
First you can change your layout on button click that you are trying to open in a new activity.for this you can use
setContentView(R.layout.SecondLayout);
again on button click and initialize your FiveActivity's resources and use them.
this saves your memory and comfusion of activities.

How do you make a new activity start when a button is clicked?

I already know how to start a one new activity when you click a button, but I have three buttons on the one layout. And I want each of the three buttons on that ONE activity to link to three other activities.
I have on the activity I have called 'main', Button 1 which is called services and I want to link it to the services activity. Button 2 which is called Search and I want it to go to the Search activity and thirdly 'map' which I want to link to the map activity.
Can someone help me do this please? Thanks
EDIT:Also, I'm a beginner with Android coding, could you explain in a little bit more detail please?
So where is problem? Just set that your class will implements View.OnClickListener and override onClick() method a you got it.
public class MainActivity extends Activity implements View.OnClickListener {
// body
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.serviceBtn:
Intent serviceIntent = new Intent(this, ServiceActivity.class);
startActivity(serviceIntent);
break;
case R.id.searchBtn:
Intent searchIntent = new Intent(this, SearchActivity.class);
startActivity(searchIntent);
break;
case R.id.mapBtn:
Intent mapIntent = new Intent(this, MapActivity.class);
startActivity(mapIntent);
break;
}
}
Tie an onclick listener to all three buttons. In the listener, retrieve the ID of the button. Make a variable of type Class. Depending on the value of the button ID, initialize it to the class of the activity to invoke. Then construct an Intent for that class, and call startActivity().
EDIT for hawaii.five-0: here's how I'd do it:
#Override
public void onClick(View view) {
Class c = null;
switch (view.getId()) {
case R.id.serviceBtn:
c = ServiceActivity.class;
break;
case R.id.searchBtn:
c = SearchActivity.class;
break;
case R.id.mapBtn:
c = MapActivity.class;
break;
}
Intent i = new Intent(YourActivity.this, c);
startActivity(i);
}
EDIT2:
class CurrentActivity extends Activity
implements OnClickListener
{
void onCreate(Bundle b)
{
//Other initialization goes here...
((Button)findViewById(R.id.MyButton1)).setOnClickListener(this);
((Button)findViewById(R.id.MyButton2)).setOnClickListener(this);
((Button)findViewById(R.id.MyButton3)).setOnClickListener(this);
}
}
Its very easy dear. Just put this code on button click event
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);
which is very easy to start a new activity when the button is clicked. I have given the example for this.
// R.id.btnAdd -> which is present in your layout page
Button btnStart = (Button) findViewById(R.id.btnAdd); // declare button
// declare listener evernt for button
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
// declare the Intent for moving another activity
Intent view = new Intent(YourCurrentClassName.this,
anotherClassName.class);
// startActivity is used to navigating the view
startActivity(view);
}
};
// set the listener evernt to button
btnStart.setOnClickListener(listener);

How can I make a button visible in one activity when clicking a radiobutton in another?

Im making a simple Androidgame , where the user selects an answer to a question (ind Activity1) by clicking a radiobutton. When the correct radiobutton is clicked, a button in the "Credits" (Activity2) will get VISIBLE and be available for the user.
How can I make this happen? i canĀ“t get the two activities to work together?
The code from Activity 1 (the Question) where the user clicks the radiobutton:
final Button s1 = (Button) findViewById(R.id.radio0);
final Button s2 = (Button) findViewById(R.id.radio1);
final Button s3 = (Button) findViewById(R.id.radio2);
s1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
btnEliminar.setVisibility(View.VISIBLE);
btnKort.setVisibility(View.VISIBLE);
s1.setVisibility(View.GONE);
s2.setVisibility(View.GONE);
s3.setVisibility(View.GONE);
AlertDialog.Builder builder = new AlertDialog.Builder(Activity1.this);
builder.setMessage("...");
builder.setCancelable(true);
builder.setPositiveButton("...", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
The code from Activity2 where the button should get visible:
public class Activity2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
Button credit1 = (Button) findViewById(R.id.buttoncredit1);
credit1.setVisibility(View.INVISIBLE);
....
credit1.setVisibility(View.VISIBLE);
Hope someone is able to help me
Thank you
This can be done via Intent's extras. Should look something like this:
//Somewhere in Activity1
Intent intent = new Intent();
intent.setClass(getApplicationContext(), Activity2.class);
intent.putExtra("makeButtonVisible", true); // Or false
startActivity(intent);
//Somewhere in Activity2
boolean isButtonVisible = getIntent().getBooleanExtra("makeButtonVisible");
// Change button's visibility accordingly
To understand the best answer to this question you must understand that each Activity in Android is essentially isolated from any other activities. This means you cannot change things such as widget visibility from one activity to another.
That said, the best solution is likely to pass extras using an Intent when you start up your 2nd activity. That can be done quite easily by following these steps.
I hope this helps!
In Activity1
s1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(this,Activity2.class);
if(s1.isChecked)
intent.putExtra("state",0);
else
intent.putExtra("state",-1);
startActivity(intent);
});
In Activity 2
Intent intent = getIntent();
int state = Integer.parseInt(intent.getExtras().get("state").toString());
credit1.setVisibility(state);
Hope this help
You can do it by different ways:
1) Implement static method in Activity2, and call it from Activity1. Don't change visibility directly - just change some static field of Activity2 and then handle it in onStart()
2) When starting Activity2, put values into Intent and then handle them in Activity2. But keep in mind, that user can use "Back" button, so you need to handle it correctly (for example, you can .finish() activity and then start new one).
First one may sounds easier, but try to avoid such static methods. Second way is better for your app design.
you should use Intent for change a activity to an other activity:
Intent intent = new Intent(thisClss.this, AnOtherClass.class);
startActivity(intent);

Categories

Resources