I am working on an app.In this,at the beginning of the app an activity will be displayed for 2 seconds and after that the app with start with the main activity.
This is how i tried to achieve this:
The activity_main.java file:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent i = new Intent(MainActivity.this,open.class);
startActivity(i);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
android.app.ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#A6250F")));
bar.setSubtitle(R.string.title_sub);
}
}
The open.java file
public class open extends Activity {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.open);
android.app.ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#A6250F")));
}
protected void onStart(){
super.onStart();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
finish();
}
}
The open.java file should be shown for 2 seconds at the beginning of the app and then the execution will continue from the activity_main.java
But what happens is a blank screen is displayed for a second and then the mainactivity is shown.Need help
You should re-design your flow so that :
open is the Activity that is created first, and 2 seconds after loads the MainActivity.
public class open extends Activity {
private Activity mActivity;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.open);
android.app.ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#A6250F")));
mActivity = this;
Handler handler = new Handler();
Runnable r = new Runnable() {
public void run() {
Intent i = new Intent(mActiviy, MainActivity.class);
startActivity(i);
}
}
handler.postDelayed(r, 2000);
}
}
Last thing is make sure you swap the Activities in your Manifest, making your application start with the open Activity
Related
I'm trying to start another activity by pressing on the cardview which has a friend finder id. But when I write home.java it gives me problems in the setOnClickListener. At homeActivity it tells me Cannot resolve method 'homeActivity' in 'HomeActivity'. because?
public class HomeActivity extends AppCompatActivity {
private CardView btn_home;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_home = findViewById(androidx.appcompat.R.id.home);
btn_home.setOnClickListener(v -> homeActivity(new Intent(HomeActivity.this, TrovamicoActivity.class)));
}
btn_home.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(HomeActivity.this, TrovamicoActivity.class);
startActivity(intent);
}
});
If there is no code in the manifest, write it
<activity android:name=".TrovamicoActivity" />
I created a project that use RecyclerView and CardView (for PointOfInterest). These 5 activities are relate to each other :
PointOfInterest.java
PlacesAdapter.java
Places.java
layout_poi.xml
activity_point_of_interest.xml
Meanwhile in activity_main.xml I design the Main Menu together with some buttons. One of the button named Rapid Penang (id: rapid_btn). I call an activity of Rapid Penang (from MainActivity.java) like below:
public class MainActivity extends AppCompatActivity {
private Button button_for_rapid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to call Rapid Penang class
button_for_rapid = (Button) findViewById(R.id.rapid_btn);
button_for_rapid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openRapid();
}
});
}
public void openRapid()
{
Intent intent_rapid = new Intent(this, RapidPenang.class);
startActivity(intent_rapid);
}
}
RapidPenang consist of only one activity and it is success. But when I try to do exactly the same to PointOfInterest activites (as mention above), suddenly the app were crashed.
This is how I try to open PointOfInterest activites from a button in MainMenu called Point Of Interest:
private Button button_for_poi;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to call Point Of Interest class
button_for_poi = (Button) findViewById(R.id.poi_btn);
button_for_poi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openPOI();
}
});
}
public void openPOI()
{
Intent intent_poi = new Intent(this, PointOfInterest.class);
Intent intent_poi2 = new Intent(this, PlacesAdapter.class);
Intent intent_poi3 = new Intent(this, Places.class);
startActivity(intent_poi);
}
Firstly check your activity is define in Android manifest file and then call
StartActivity(new Intent(getApplicationcontext(),RapidPenang.class));
That's it
Probably something I do not understand.
In the application programs (from google play), if we move from the main activity to the second and then back to the main, then after pressing the "back" button on the phone, the application closes.
I tried to make my own applications with two activities, but it did not work as it should. When the main activity goes to the second and then I go back to the main, then after pressing the "back" button on the phone, the application instead of closing, it goes back to the second activity, then back to the main and it just closes.
What am I doing wrong ?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button Act2Butt = (Button) findViewById(R.id.Act2Butt);
Act2Butt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
});
}
.
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Button Act1Butt = (Button) findViewById(R.id.Act1Butt);
Act1Butt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Main2Activity.this, MainActivity.class );
startActivity(intent);
}
});
}
}
To go back to MainActivity from Main2Activity, you need to call onBackPressed() or finish(), instead of startActivity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.title = (String) getTitle();
// Getting reference to the DrawerLayout
this.drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
this.drawerList = (ListView) findViewById(R.id.drawer_list);
this.fragments[0] = new My_Main_Fragment(this);
this.fragments[1] = new My_Breakfast_Fragment();
this.fragments[2] = new My_Lunch_Fragment();
this.fragments[3] = new My_Dinner_Fragment();
this.fragments[4] = new My_Dessert_Fragment();
this.fragments[5] = new My_Seat_Plan_Fragment();
this.fragments[6] = new My_Beverages_Fragment();
this.item_titles = getResources().getStringArray(R.array.menu_items);
.................................................................................................................................................................................................................
public class My_Seat_Plan_Fragment extends Activity{
Button create, edit, view, delet, find;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.air_main_interface);
create = (Button)findViewById(R.id.note_add);
edit = (Button)findViewById(R.id.note_edit);
view = (Button)findViewById(R.id.note_view);
delet = (Button)findViewById(R.id.note_delete);
find = (Button)findViewById(R.id.note_find);
create.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(My_Seat_Plan_Fragment.this,Seat_create.class);
startActivity(in);
}
});
................................................................................................
In below attached two classes i need to connect the Main Activity with the fragment class...
Main Activity is followed as extends Activity implements My_Fragments_Parent {
and Fragment Activity is as followed as public class My_Seat_Plan_Fragment extends Activity{
but im getting an error in the main activity in the line
this.fragments[5] = new My_Seat_Plan_Fragment(); ------ saying cannot convert from My Seat_Plan_Fragment to Fragment
So if I change My seat_Plan Fragment Extends Activity then button activities cannot be done...
If I understand the question, you can use something like this in your fragment:
((MainActivity) getActivity()).someMethod();
So I have this code in my main activity to start a new one:
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.GoButton).setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent(MainActivity.this, NewActivity.class);
MainActivity.this.startActivity(myIntent);
//finish();
}
});
}
}
My new activity extends ListActivity and when I call this code by pressing the button it crashes the application. However if I make the MainActivity extend ListActivity then it works great (although I have to replace the button with a List!). Does anyone know why this happens, and how can I make it work using the code above?
Thanks
Have you added the manifest entry