I have a button which I am assigning an OnClickListener to. I want to start a new intent as a result of this. In order to do that, I have to reference the activity. The only one I know how to do this is via something like the following code. Is this the best way to start an intent from a button click? Also, what kind of memory implications will there be for this?
public class SomeActivity extends FragmentActivity {
private final FragmentActivity self=this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.startButton).setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent intent=new Intent(self,someClass.class);
startActivity(intent);
}
});
}
The best way to do this is to simply pass ActtivityName.this, like this:
public class SomeActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.startButton).setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent intent=new Intent(SomeActivity.this,someClass.class);
startActivity(intent);
}
});
}
Another way to do this will be calling :
findViewById(R.id.startButton).setOnClickListener(this);
Then make your Activity implement View.OnClickListener and implement the methods onClick(View v) in this way :
public void onClick(View v) {
switch(v.getId()) {
case R.id.startButton:
Intent intent=new Intent(self,someClass.class);
startActivity(intent);
break;
// Handle click on other views
}
}
It prevents from instantiating a listener and should be a bit better for memory consumption.
Related
In a project there will be lots of pages(activities) and user will be able to switch between these pages (activities). So when user press the corresponding button that opens page_2 from page_1, I need to create a new Activity. However, if user comes back to page_1 and try to open the page_2 again, there will be a new Activity created again, instead of opening the previously created activity ( I want user to see the page_2 as he/she left it without anychanges). So I want to put something like
if(SecondActivity==null)
{
//Create new activity
}
start(new_activity);
Here is the corresponding code ( I couldn't implement onClickListener because I couldn't disable it in onPause() method... so I used onClick from xml)
public class MainActivity extends Activity {
private View.OnClickListener openSecondPage = new View.OnClickListener() {
#Override
public void onClick(View v) {
Button button_newPage = findViewById(R.id.button_newpage);
button_newPage.setText("Clicked");
Intent secondPage = new Intent(getApplicationContext(), SecondActivity.class );
startActivity(secondPage);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onResume(){
super.onResume();
//Button button_newPage = findViewById(R.id.button_newpage);
// button_newPage.setOnClickListener(openSecondPage);
}
public void onPause(){
super.onPause();
//Destroy the on click listener
Button button_newPage = findViewById(R.id.button_newpage);
// button_newPage.setOnClickListener(null);
}
public void openSecondPage(View v)
{
Intent secondPage = new Intent(getApplicationContext(), SecondActivity.class );
startActivity(secondPage);
}
}
Edit: Here is the new code with Flags:
MainActivity.java code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onResume(){
super.onResume();
}
public void onPause(){
super.onPause();
}
public void openSecondPage(View v)
{
Intent secondPage = new Intent(getApplicationContext(), SecondActivity.class );
secondPage.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
startActivity(secondPage);
}
}
SecondActivity.java code:
public class SecondActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity);
}
protected void onResume()
{
super.onResume();
}
public void goBack(View v)
{
}
public void goMainPage(View v)
{
Intent mainPage = new Intent(getApplicationContext(),MainActivity.class);
mainPage.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
startActivity(mainPage);
}
}
I think you can resolve problem by FLAG_ACTIVITY_REORDER_TO_FRONT flag.
You need to set flag when start activity.
public void openSecondPage(View v) {
Intent secondPage = new Intent(getApplicationContext(), SecondActivity.class);
secondPage.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(secondPage);
}
You can find more detailed information in this link.
https://developer.android.com/guide/components/activities/tasks-and-back-stack
Show Activity A, then when you need it then show Activity B, then when you need it then show Activity A again, by calling startActivity + setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) => Activity A will show up as it was exactly before showing Activity B
According to your question asked, it seems you have to use either Room or SharedPreference in SecondActivity to save the user input data.
So, whenever you come back to SecondActivity, check if there is already data available. If there is a data available, show it to the user else set the fields with default values.
I'm having an Issue that I have two Activities A and B when I clicked on Button to change an Activity from A to B . It Restart my Application I don't know what going wrong Please help me
public class Login_Choice_Activity extends AppCompatActivity{
private Button d_btn,p_btn;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login__choice);
FindAllView();
p_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// startActivity(new
Intent(Login_Choice_Activity.this,Patient_SignIn_Activity.class));
Toast.makeText(getApplicationContext(),"CLICKED",Toast.LENGTH_LONG).show();
}
});
}
private void FindAllView(){
d_btn = findViewById(R.id.choice_doctor_btn);
p_btn = findViewById(R.id.choice_patient_btn);
}
}
https://i.stack.imgur.com/KcgAh.gif
It could be better if you create a method for opening the new Activity and call it from onClick(View v) method. Example
private void openActivity() {
startActivity(new Intent(this,Patient_SignIn_Activity.class));
}
In your onClick call:
#Override
public void onClick(View v) {
openActivity()
}
I devised some activities with its relationships between each other after I pass any acttivity from Main Actvity. But I couldn't succeed to pass another activity from any activity.
Here is the code below.
public class A extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_istanbul);
TextView attractivePlaces = (TextView) findViewById(R.id.a_category_attractive_places);
attractivePlaces.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"A",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(A.this,B.class); // Here the issue is.
startActivity(intent);
}
});
}
}
How can I solve the problem.
Try changing View.OnClickListener() to TextView.OnClickListener()
final code would be like this:
public class A extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_istanbul);
TextView attractivePlaces = (TextView) findViewById(R.id.a_category_attractive_places);
attractivePlaces.setOnClickListener(new TextView.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"A",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(A.this,B.class); // Here the issue is.
startActivity(intent);
}
});
}
}
Maybe this will help.
I have two java files Main and New. In main i have a number methods from oncreate to surface destroyed. on clicking one of the button it goes to New. There i have called the same xml file. But no methods are working there. i want to call the oncreate method including all methods from New. Please help
public class Main extends Activity implements SurfaceHolder.Callback {
#Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.main);
...............}
buttonStartCameraPreview.setOnClickListener(new Button.OnClickListener()
{
#Override
public void onClick(View v)
{......}
});
testButton.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v)
{
Intent nextIntent=new Intent(getApplicationContext(),New.class);
startActivityForResult(nextIntent,301);
}
});
Second class
public class New extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Your second class name is NewActivity
but Your intent is calling different class.
->Intent nextIntent=new Intent(getApplicationContext(),New.class);
replace to
Intent nextIntent=new Intent(getApplicationContext(),NewActivity.class);
You can also create a base activity that both MainActivity and NewActivity extends and put methods in it.
For ex:
public class BaseActivity extends Activity{
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.main);
...............}
buttonStartCameraPreview.setOnClickListener(new Button.OnClickListener()
{
#Override
public void onClick(View v)
{......}
});
testButton.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v)
{
Intent nextIntent=new Intent(getApplicationContext(),NewActivity.class);
startActivityForResult(nextIntent,301);
}
});
}
public class MainActivity extends BaseActivity{
onCreate..
{
...
}
}
public class NewActivity extends BaseActivity{
onCreate..
{
...
}
}
I have an Android App That requires a button to open up a new xml page. This is what it's like now, could someone add the necessary code to make it open Page2Activity when I click on the button? Code:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Void onClick;View arg0; {
// TODO Auto-generated method stub
}
};
}
I figured this out using this method : http://stackoverflow.com/questions/4094103/linking-xml-pages-with-layout ,but I will try All of yours as well.
Try this code:
public void handleClick(View v){
//Create an intent to start the new activity.
Intent intent = new Intent();
intent.setClass(this,Page2Activity.class);
startActivity(intent);
}
Then create a new cLass called Page2Activity.
Hope this helps and don't forget to add your activity to the manifest file.
i think you mean something like this:
public class MyClass extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent("com.myaction");
startActivity(i);
}
});
}
}