I have a very heavy Main Acitivty class which the first time you install the app , freezes until all the data is loaded , I want to show my Splash Screen activity while all the data is loading in the Main Activity and show my activity ONLY when the Main Activity has loaded everything :
Here is my current splash screen activity , currently it only does this:
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
How can I achieve this?
Regarding the comments on your question, it seems you're trying to update the UI of your MainActivity with the data you're loading. You can query/load your data on the splash activity, pass it on to the intent so you can gather it in onCreate of your MainActivity, or save all your data to SharedPreferences and access it from the MainActivity.
In SplashActivity:
Intent intent = new Intent(this, MainActivity.class);
// inside data loading completion callback or after synchronous data gathering methods
intent.putExtra("key","value");
startActivity(intent);
In MainActivity onCreate method
Bundle extras = intent.getExtras();
String value = extras.getString("key");
You can pass models as json formatted strings if you need.
if your splash screen named: spalsh.java
and your main activity named: MainActivity.java
first you create this class:
public class SliderPrefManager {
private Context context;
private SharedPreferences pref;
private static final String Pref_Name="slider-pref";
private static final String Key_Start="startslider";
public SliderPrefManager(Context context){
this.context = context;
pref = context.getSharedPreferences(Pref_Name,Context.MODE_PRIVATE);
}
public Boolean startSlider(){
return pref.getBoolean(Key_Start,true);
}
public void setStartSlider(Boolean start){
pref.edit().putBoolean(Key_Start,start).apply();
}
}
and in your splash screen add this code:
sliderPrefManager = new SliderPrefManager(login_Activity.this);
sliderPrefManager.setStartSlider(false);
and you checked boolean of view slash screen, add this code in your mainActivity:
if (sliderPrefManager.startSlider()) {
Intent intent = new Intent(choise_way_sec.this, login_Activity.class);
startActivity(intent);
finish();
}
Related
I am storing an intent in shared preferences like so :
new Intent(context, MainActivity.this);
and then when I try to open it I am getting the error :
android.content.ActivityNotFoundException: No Activity found to handle Intent { }
I assume it's because context is referring to the stored activity? when context should be the current activity.
My question is how do I get around this one? I have several buttons which is populated by objects stored in the preferences. Each of them opens different activities. Basically I am mapping activities to be opened to buttons.
To give you an idea :
public class Scene implements Serializable {
public String label;
public Intent intent;
public boolean isUnlocked;
public Scene(String lbl, Intent i) {
this.label = lbl;
this.intent = i;
}
}
for (Scene scene : savedScenesFromSharedPreferences) {
Button btn = new Button();
btn.setOnClickListener(new OnClickListener()) {
startActivity(scene.intent);
}
}
You can try this:
In your Scene class, instead of saving the Intent, save the activity name.
scene.setLabel("Button A");
scene.setActivityName("com.example...YourOtherActivityName");
To start the activity:
startActivity(new Intent(YourCurrentActivity.this, Class.forName(scene.getActivityName)));
I made one Fragment (ListA) with a ListView. When I click one item of the ListView I want to start the MainActivity.
public class ListA extends Fragment {
private WearableListView ListV;
...
private WearableListView.ClickListener mClickListener = new WearableListView.ClickListener() {
#Override
public void onClick(WearableListView.ViewHolder viewHolder) {
Intent intent= new Intent(getActivity().getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("timestamp", "AAA");
startActivity(intent);
}
This is the MainActivity:
public class MainActivity extends Activity {
#Override
public void onResume() {
super.onResume();
if(getIntent().getStringExtra("timestamp")!=null){
Toast.makeText(getApplicationContext(),"Has Timestamp",Toast.LENGTH_SHORT).show();
}
}
When I add flag Intent.FLAG_ACTIVITY_SINGLE_TOP, getIntent().getStringExtra("timestamp") returns null. If I add flag Intent.FLAG_ACTIVITY_NEW_TASK it is not null.
I want to have flag Intent.FLAG_ACTIVITY_SINGLE_TOP and it returns null, I don't know why.
I think because MainActivity was already started before so no new intents is being passed to it.
Try destroying the main activity before you go to list activity. See if this works
Currently I have the following code:
public class GameMenu extends Activity{
//some code
public void showOptions(View view){
if(view.equals(R.id.optionsButton){
Intent intent = new Intent(this, OptionsMenu.class);
intent.putExtra("FACADE",this.gameFacade);
startActivity(intent);
}
}
}
OptionsMenu
public class OptionsMenu extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_options_controller);
Intent calledFromIntend = getIntent();
this.facade = (Facade) calledFromIntend.getSerializableExtra("facade");
}
//some more code
}
Is it possible that the facade is the same in class GameMenu and OptionsMenu?
Because now it's a copy, so if the user checks option x in OptionsMenu, it isn't known in GameMenu.
Its not the same because it gets deserialized from the one you pass in the Intent. You can use startActivityForResult instead of startActivity in the GameMenu to get a result back from OptionsMenu. See http://developer.android.com/training/basics/intents/result.html for more info.
how to pass 2D Array as a parameter to another Activity?? i try it stack over flow solution but is not work im using this code
in activity 1 is correctly show value in this line bundle.putSerializable("xmlResponee", xmlRespone);
but is not showe value in activity2 class what is wrong? tell me please
public class Activity1 extends Activity {
private String[][] xmlRespone;
Intent i = new Intent(this.getApplicationContext(), Activity2.class);
Bundle bundle = new Bundle();
bundle.putSerializable("xmlResponee", xmlRespone);
i.putExtras(bundle);
startActivity(i);
and
public class Activity2 extends Activity {
private String[][] xmlRespone;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
Bundle bundle = getIntent().getExtras();
String[][] xmlRespone2 = (String[][]) bundle.getSerializable("xmlResponee");
You can make a static class.. with private String[][] xmlRespone. in first Activity you can Assign value to it,and in another activity you can call data from it..
Activity A ---> Static class X ---> Activity B.
Hey just use Parcelable objects to pass objects between android activities.
Here is a really awesome example. I guess this is the same thing you want to achieve.
You can put it directly on the intent no need bundle.
Intent intent = new Intent();
intent.putExtra("MyXML", xmlRespone);
And to read:
#Override
public void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
String[] xmlRespone = intent.getStringArrayExtra("MyXML");
}
I've made a separate class to launch and intent as the class I would like to launch the intent from is a thread and does not inherit from activity and would not launch startActivity. Every time I launch the app I get a null pointer exception for the context.
public class ToLaunch extends Activity {
public void launchScoreloop() {
con.getApplicationContext();
startActivity(new Intent(this, LeaderboardsScreenActivity.class));
}
}
You Are writing an Activity , and you didn't override the method onCreate().
public class ToLaunch extends Activity {
#override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Call your method here after a button click cor example or something else
}
public void launchScoreloop() {
con.getApplicationContext();
startActivity(new Intent(this, LeaderboardsScreenActivity.class));
}
}
refer this two tutorials about using intents to start another Activity :
tuto 1
tuto 2
And if you want to launch the Activity from another Class , you should pass the context to the second Class like this :
SecondClass instance = new SecondClass(this);
and the contructor of your SecondClass will be something like this :
public void SecondClass(Context _context){
this.context = _context;
}
and then you can start the Avtivity by using the context that you passed to your SecondClass like this :
this.context.startActivity(....);
If thread is a inner class inside your activity you can use
startActivity(new Intent(YourActivity.this, LeaderboardsScreenActivity.class));
If it is a separate class you can make a constructor that take context has constructor as argument and you can pass your activity context into that constructor
Context con;
public YourThread(Context context){
con = context;
}
and from inside your activity, while making thread object
YourThread thread = new YourThread(this);